n; if(n,c++,C++" /> n; if(n,c++,C++" />

为什么标识符是;v";未定义? 我对编程很陌生,我正在学习C++。我试图在这个练习中使用向量,但它们让我非常困惑。我认为这段代码应该可以工作,但我不知道为什么我的IDE说v是未定义的 #include <iostream> #include <string> #include <vector> using namespace std; class check_input { public: void checkInput(); void getnum(); void displaynum(); private: vector<int> x; int sum = 0; int n = -1; }; void check_input::checkInput() { sum = 0; n = -1; } void check_input::getnum() { int x; cout << "Please enter the number of values you want to sum, starting with the first: "; cin >> n; if (n < 1) { cout << "the number of elements must be a positive integer" << endl; } else { cout << "Please enter some integers (press '|' to stop): "; while (cin >> x) v.push_back(x); if (v.size() < n) { cout << "too few numbers; we need " << n << endl; } else { for (int i = 0; i < n; ++i) sum += v[i]; } } } void check_input::displaynum() { cout << "The sum of the first " << n << " numbers ( "; for (int i = 0; i < n; ++i) cout << v[i] << ' '; cout << ") is " << sum << '\n'; } #包括 #包括 #包括 使用名称空间std; 类检查输入 { 公众: void checkInput(); void getnum(); void displaynum(); 私人: 向量x; 整数和=0; int n=-1; }; 无效检查输入::检查输入() { 总和=0; n=-1; } 无效检查_输入::getnum() { int x; cout>n; if(n

为什么标识符是;v";未定义? 我对编程很陌生,我正在学习C++。我试图在这个练习中使用向量,但它们让我非常困惑。我认为这段代码应该可以工作,但我不知道为什么我的IDE说v是未定义的 #include <iostream> #include <string> #include <vector> using namespace std; class check_input { public: void checkInput(); void getnum(); void displaynum(); private: vector<int> x; int sum = 0; int n = -1; }; void check_input::checkInput() { sum = 0; n = -1; } void check_input::getnum() { int x; cout << "Please enter the number of values you want to sum, starting with the first: "; cin >> n; if (n < 1) { cout << "the number of elements must be a positive integer" << endl; } else { cout << "Please enter some integers (press '|' to stop): "; while (cin >> x) v.push_back(x); if (v.size() < n) { cout << "too few numbers; we need " << n << endl; } else { for (int i = 0; i < n; ++i) sum += v[i]; } } } void check_input::displaynum() { cout << "The sum of the first " << n << " numbers ( "; for (int i = 0; i < n; ++i) cout << v[i] << ' '; cout << ") is " << sum << '\n'; } #包括 #包括 #包括 使用名称空间std; 类检查输入 { 公众: void checkInput(); void getnum(); void displaynum(); 私人: 向量x; 整数和=0; int n=-1; }; 无效检查输入::检查输入() { 总和=0; n=-1; } 无效检查_输入::getnum() { int x; cout>n; if(n,c++,C++,因为您没有定义该变量。 请尝试在正确的范围内定义它,您将不会得到IDE的错误。您没有定义向量v,将向量v;添加到您的代码中那么v在哪里定义?您应该问程序员,他们为什么没有定义v。我们不知道为什么没有定义它。

因为您没有定义该变量。


请尝试在正确的范围内定义它,您将不会得到IDE的错误。

您没有定义向量v,将
向量v;
添加到您的代码中

那么
v
在哪里定义?您应该问程序员,他们为什么没有定义
v
。我们不知道为什么没有定义它。