[英]C++ errors initializing variable - Double initialization and Missing initialization [closed]
我是新的 c++ 编程,非常感谢您的帮助。
我正在制定迭代投票计划。 我有一个 class 名称 PrefList 和一个变量名称 vote:
protected:
int candidateNumber;
int* vote;
我还有初始化这个变量的构造函数,然后调用这个使用变量的方法:
PrefList::PrefList(const PrefList & rhs) { // constructor
candidateNumber = rhs.getCandidateNumber();
this->vote = new int[candidateNumber]; // _____ My first Problem Is Here_____
populateVoteArray(rhs.getVote());
}
void PrefList::populateVoteArray(const int * newVote) {
for (int i = 0; i < candidateNumber; i++) {
usedCandidates[newVote[i]] = true;
this->vote[i] = newVote[i]; // _____ My second Problem Is Here_____
}
}
在当前情况下,我在第一个问题标记的行上收到错误:
ConsoleApplication2.exe 中 0x7647CA42 处未处理的异常:Microsoft C++ 异常:memory 位置 0x006FE424 处的 std::bad_array_new_length。
这就是说,据我所知,变量“投票”有一个双重初始化,因为还有另一个构造函数初始化这个变量。 但是当我删除这个初始化行时,我在第二个问题标记的行上得到了这个错误:
抛出未处理的异常:写访问冲突。 这个->投票是0xCEDECEDF。
这就是说,据我了解,变量“投票”没有初始化。
初始化变量的好方法是什么,我该如何解决这个问题?
提前致谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.