我收到的异常是
Exception thrown at 0x70F8516F (vcruntime140d.dll) in Project.exe: 0xC0000005: Access violation writing location 0x723D9C18.
它发生在用户定义的信息在 for 循环中最终迭代到数组中时:
int k;
cout << "Enter array size:";
cin >> k;
while (k > 3) {
cout << "Array size too big, please reenter" << endl;
cin >> k;
}
Player *ptr = new Player[k];
string n;
int s;
for (int i = 0; k >= i; i++) {
cout << "Enter name" << endl;
cin >> n;
ptr[i].setName(n);
cout << "Enter score" << endl;
cin >> s;
ptr[i].setScore(s);
ptr[i].getName();
ptr[i].getScore();
}
它指引我到 setName 函数的末尾
void Player::setName(string n) {
name = n;
}
最佳答案
你的数组大小应该是 (k+1) 或者 for 循环应该是这样的:
for (int i = 0; i<k; i++) {
cout << "Enter name" << endl;
cin >> n;
ptr[i].setName(n);
cout << "Enter score" << endl;
cin >> s;
ptr[i].setScore(s);
ptr[i].getName();
ptr[i].getScore();
}
关于c++ - 在循环期间收到 "Exception Thrown",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196913/