【问题标题】:redeclare vector variable cause error in c++重新声明向量变量导致c ++中的错误
【发布时间】:2022-08-17 15:39:47
【问题描述】:

我将分数声明为向量。 然后我将 score 重新声明为 int 数据类型。 为什么 cin 会导致错误?

#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
    vector<int> score;
    int score;
    cout<<\"\\nEnter a score to find\";
    cin>>score;
    cout<<score;
}

为什么这段代码不会导致错误?

#inlcude<iostream>
using namespace std;
int main()
{
    int score;
    float score;
    cin>>score;
    cout<<score;
}

标签: c++


【解决方案1】:

与 python 不同,您不能这样做,除非您将变量放在不同的范围.例如,

#inlcude<iostream>
int main()
{
    {
        int score;
    }/*the previously declared score is out of scope here*/
    float score;
    cin>>score;
    cout<<score;
}

【讨论】:

    猜你喜欢
    • 2019-01-07
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多