[英]C++ Exception Thrown while trying to debug
I am trying to learn C++ with a given tutorial.我正在尝试通过给定的教程学习 C++。
I've tried to write some code.我试着写一些代码。 Visual studio says, there's no error, but when I'm trying to start debugging, it does not work.
Visual Studio 说,没有错误,但是当我尝试开始调试时,它不起作用。 Can someone help me.
有人能帮我吗。
I am getting the following Exception thrown.我正在抛出以下异常。
Here's the error message:这是错误消息:
https://prnt.sc/1x6qqgc https://prnt.sc/1x6qqgc
#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;
}
You have out-of-array-boundries access everywhere:您可以在任何地方进行数组外访问:
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]);
Note: Counting in arrays starting at index 0 means if you define string choices[3];
注意:从索引 0 开始计算数组意味着如果您定义
string choices[3];
that means you have 3 strings at choices[0]
, choices[1]
& choices[2]
这意味着你有 3 个字符串
choices[0]
、 choices[1]
和choices[2]
It's solved, thank you for your helps.已解决,谢谢您的帮助。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.