尝试调试时抛出 C++ 异常
C++ Exception Thrown while trying to debug
我正在尝试通过给定的教程学习 C++。
我试着写了一些代码。
Visual studio 说,没有错误,但是当我尝试开始调试时,它不起作用。
谁能帮帮我。
我收到以下异常抛出。
错误信息如下:
#include <iostream>
#include <string>
using namespace std;
class Question
{
private:
string text;
string choices[3];
string answer;
public:
Question(string text, const string choices[3], string answer)
{
this->text = text;
this->choices[3] = choices[3];
this->answer = answer;
}
bool checkAnswer(string answer)
{
return this->answer == answer;
}
string getText()
{
return this->text;
};
};
class Quizz
{
private:
Question* questions[5];
int score = 0;
int questionIdx = 0;
public:
Quizz(Question questions)
{
this->questions[5] = &questions;
};
Question* getQuestions()
{
return questions[questionIdx];
};
void displayQuestion()
{
Question* questions = getQuestions();
cout << questionIdx + 1 << " / " << 5 << "Question : " << questions->getText();
};
};
int main()
{
const string cho[3] = { "Carl","Mike","Jason" };
Question q1 = {"Who has a dog?", cho, "Carl"};
Question q2 = { "Who has a cat", cho , "Mike" };
Question q3 = { "Who knows i have a cat ", cho , "Mike" };
Question q4 = { "Who knows i haven't got a dog", cho , "Carl" };
Question q5 = { "Who knows i live in LA", cho , "Jason" };
Question questions[5] = { q1,q2,q3,q4,q5 };
Quizz quiz(questions[5]);
quiz.displayQuestion();
return 0;
}
你到处都有数组边界外的访问:
this->choices[3] = choices[3]; --> You can only access up to choices[2]
this->questions[5] = &questions; --> You can only access up to questions[4]
Quizz quiz(questions[5]);
注意:在从索引 0 开始的数组中计数意味着如果您定义 string choices[3];
,则意味着您在 choices[0]
、choices[1]
和 choices[2]
[=15 处有 3 个字符串=]
已经解决,谢谢大家的帮助。
#include <iostream>
#include <string>
using namespace std;
class Question
{
private:
string text;
string choices[3];
string answer;
public:
Question(string textt, string choices[] , string answerr)
{
text = textt;
for (int i = 0; i < 3 ; i++) {
this->choices[i] = choices[i];
}
answer = answerr;
}
bool checkAnswer(string answer)
{
return this->answer == answer;
}
string getText()
{
return this->text;
};
};
class Quizz
{
private:
Question* questions[5];
int score = 0;
int questionIdx = 0;
public:
Quizz(Question questions[])
{
for (int i = 0; i < 5; i++)
{
this->questions[i] = &questions[i];
}
};
Question* getQuestions()
{
return questions[questionIdx];
};
void displayQuestion()
{
Question* questions = getQuestions();
cout << questionIdx+1 << " / " << 5 << "Soru : " << questions->getText();
};
};
int main()
{
const string cho[3] = { "Carl","Mike","Jason" };
Question q1 = {"Who has a dog?", cho, "Carl"};
Question q2 = { "Who has a cat", cho , "Mike" };
Question q3 = { "Who knows i have a cat ", cho , "Mike" };
Question q4 = { "Who knows i haven't got a dog", cho , "Carl" };
Question q5 = { "Who knows i live in LA", cho , "Jason" };
Question questions[5] = {q1,q2,q3,q4,q5};
Quizz quiz(questions);
quiz.displayQuestion();
return 0;
}
我正在尝试通过给定的教程学习 C++。
我试着写了一些代码。 Visual studio 说,没有错误,但是当我尝试开始调试时,它不起作用。 谁能帮帮我。
我收到以下异常抛出。
错误信息如下:
#include <iostream>
#include <string>
using namespace std;
class Question
{
private:
string text;
string choices[3];
string answer;
public:
Question(string text, const string choices[3], string answer)
{
this->text = text;
this->choices[3] = choices[3];
this->answer = answer;
}
bool checkAnswer(string answer)
{
return this->answer == answer;
}
string getText()
{
return this->text;
};
};
class Quizz
{
private:
Question* questions[5];
int score = 0;
int questionIdx = 0;
public:
Quizz(Question questions)
{
this->questions[5] = &questions;
};
Question* getQuestions()
{
return questions[questionIdx];
};
void displayQuestion()
{
Question* questions = getQuestions();
cout << questionIdx + 1 << " / " << 5 << "Question : " << questions->getText();
};
};
int main()
{
const string cho[3] = { "Carl","Mike","Jason" };
Question q1 = {"Who has a dog?", cho, "Carl"};
Question q2 = { "Who has a cat", cho , "Mike" };
Question q3 = { "Who knows i have a cat ", cho , "Mike" };
Question q4 = { "Who knows i haven't got a dog", cho , "Carl" };
Question q5 = { "Who knows i live in LA", cho , "Jason" };
Question questions[5] = { q1,q2,q3,q4,q5 };
Quizz quiz(questions[5]);
quiz.displayQuestion();
return 0;
}
你到处都有数组边界外的访问:
this->choices[3] = choices[3]; --> You can only access up to choices[2]
this->questions[5] = &questions; --> You can only access up to questions[4]
Quizz quiz(questions[5]);
注意:在从索引 0 开始的数组中计数意味着如果您定义 string choices[3];
,则意味着您在 choices[0]
、choices[1]
和 choices[2]
[=15 处有 3 个字符串=]
已经解决,谢谢大家的帮助。
#include <iostream>
#include <string>
using namespace std;
class Question
{
private:
string text;
string choices[3];
string answer;
public:
Question(string textt, string choices[] , string answerr)
{
text = textt;
for (int i = 0; i < 3 ; i++) {
this->choices[i] = choices[i];
}
answer = answerr;
}
bool checkAnswer(string answer)
{
return this->answer == answer;
}
string getText()
{
return this->text;
};
};
class Quizz
{
private:
Question* questions[5];
int score = 0;
int questionIdx = 0;
public:
Quizz(Question questions[])
{
for (int i = 0; i < 5; i++)
{
this->questions[i] = &questions[i];
}
};
Question* getQuestions()
{
return questions[questionIdx];
};
void displayQuestion()
{
Question* questions = getQuestions();
cout << questionIdx+1 << " / " << 5 << "Soru : " << questions->getText();
};
};
int main()
{
const string cho[3] = { "Carl","Mike","Jason" };
Question q1 = {"Who has a dog?", cho, "Carl"};
Question q2 = { "Who has a cat", cho , "Mike" };
Question q3 = { "Who knows i have a cat ", cho , "Mike" };
Question q4 = { "Who knows i haven't got a dog", cho , "Carl" };
Question q5 = { "Who knows i live in LA", cho , "Jason" };
Question questions[5] = {q1,q2,q3,q4,q5};
Quizz quiz(questions);
quiz.displayQuestion();
return 0;
}