gpt4 book ai didi

c++ - 在循环期间收到 "Exception Thrown"

转载 作者:行者123 更新时间:2023-11-28 05:22:26 25 4
gpt4 key购买 nike

我收到的异常是

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com