为什么标识符"v"未定义?
Why is identifier "v" undefined?
我对编程很陌生,我正在学习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';
}
因为你没有定义这个变量
请尝试在右作用域中定义它,这样您就不会得到IDE的错误。:)
您没有定义向量v,请在代码中添加vector<int> v;
相关文章:
- 为什么我会收到警告,指出函数已使用但未定义,以及已定义但未使用?
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 为什么我会收到链接器错误:未定义对 ..?
- 为什么 setjmp/longjmp 的这种用法是未定义的行为?
- 为什么我在 Windows API 中得到对 TextOut() 函数的未定义引用?
- 为什么我的C++代码无法编译,出现未定义的引用错误
- 为什么更改包含 psapi.h 的顺序会产生编译错误?(标识符 BOOL 未定义)
- 为什么当函数参数未定义为常量引用时存在无限递归?
- 为什么销毁被放置 new 覆盖的对象不是未定义的行为?
- 未定义的对象(〔basic.life〕/8):为什么允许引用重新绑定(和常量修改)
- 为什么这种类型的双关语不是未定义的行为?
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- 为什么T是未定义的?我正在尝试实现一个用于双链表的节点类,它不喜欢我使用友元运算符后的T
- 为什么 consteval 函数允许未定义的行为?
- 为什么我总是收到错误:"未定义对'robots::robots()'的引用
- 为什么虚拟方法生成对_sbrk的未定义引用?
- 为什么 mfc 中静态文本工具中的变量未定义
- 为什么 std::memcpy(作为类型双关语的替代方案)不会导致未定义的行为?
- 为什么 i = ++i + 2 是未定义的行为?
- 为什么我的 2D 数组未定义?