值没有正确传递?
我已经在这个项目上工作了一段时间,这对我来说是一门新语言,但我有一个有经验的合作伙伴,他有点抛弃了我。不管怎样,现在我在将文本从一个对象传递到另一个对象时遇到了麻烦。我在游戏类中实例化一个对象,然后尝试获取它并将其保存到主类中的另一个对象,但是当我获取该对象时它是空的!我不知道这里发生了什么事,而且我似乎无法弄清楚。
但当我尝试绘制问题文本时,不起作用的部分是在显示方法中:
drawText((WinWidth/2)-225, (WinHeight/2) - 90, curQuestion.question.c_str());
curQuestion 在顶部创建,但在鼠标方法中实例化:
curQuestion = g.getQuestion(col,row);
这是游戏类(位于 Cc.h 中)
class Game {
public:
Game(bool);
void initQuestions();
Question getQuestion(int, int);
string getQuestionText(int, int);
private:
Question questions[5][5];
};
Game::Game(bool m)
{
mp = m;
initQuestions();
}
void Game::initQuestions()
{
bool hasDouble = false;
srand( time(NULL));
int blarg = rand() % 25 + 1;
fstream questionFile;
questionFile.open("questions.txt", ifstream::in);
int cur = 0;
for(int c = 0; c < 5; c++)
{
for(int r = 0; r < 5; r++)
{
char * q = new char[256];
char * a = new char[256];
questionFile.getline(q,256);
questionFile.getline(a,256);
questions[c][r] = Question(c,r, false, q, a);
cout << questions[c][r].question.c_str() << questions[c][r].answer.c_str();
}
}
questionFile.close();
}
Question Game::getQuestion(int c, int r)
{
return questions[c][r];
}
string Game::getQuestionText(int c, int r)
{
return questions[c][r].question;
}
注意:游戏方法中调用的 cout 确实返回了它应该返回的内容!
问题类:
class Question {
public:
int col;
int row;
bool dailyDouble;
string question;
string answer;
int value;
Question();
Question(int, int, bool, string, string);
bool checkAnswer(string);
string getQuestion();
};
Question::Question() { }
Question::Question(int c, int r, bool d,string q, string a)
{
col = c; row = r; dailyDouble = d; question = q, answer = a;
cout << "TEST> Q: " << question << ", A: " << answer << endl;
if(d)
value = r * 200 * 2;
else
value = r * 200;
}
bool Question::checkAnswer(string answer)
{
if(answer.find("What is") && answer.find(answer))
return true;
return false;
}
string Question::getQuestion() {
return question;
}
我真的不明白这里出了什么问题,非常感谢任何帮助。我希望一旦我弄清楚这里出了什么问题,我就能自己完成!
I've been working on this project for a while, it's a new language for me but I had a partner with experience who kinda ditched me. Anyways, right now I'm having trouble with getting the text from one object to another. I instantiate an object in the game class, then try to get it and save it to another object in the main class but when I get the object it's empty! I don't know what's going on here and I just can't seem to figure it out.
but the part that isn't working is in the display method when I try to draw the questions text:
drawText((WinWidth/2)-225, (WinHeight/2) - 90, curQuestion.question.c_str());
curQuestion is created at the top but instantiated in the mouse method:
curQuestion = g.getQuestion(col,row);
and here's the game class (which is in Cc.h)
class Game {
public:
Game(bool);
void initQuestions();
Question getQuestion(int, int);
string getQuestionText(int, int);
private:
Question questions[5][5];
};
Game::Game(bool m)
{
mp = m;
initQuestions();
}
void Game::initQuestions()
{
bool hasDouble = false;
srand( time(NULL));
int blarg = rand() % 25 + 1;
fstream questionFile;
questionFile.open("questions.txt", ifstream::in);
int cur = 0;
for(int c = 0; c < 5; c++)
{
for(int r = 0; r < 5; r++)
{
char * q = new char[256];
char * a = new char[256];
questionFile.getline(q,256);
questionFile.getline(a,256);
questions[c][r] = Question(c,r, false, q, a);
cout << questions[c][r].question.c_str() << questions[c][r].answer.c_str();
}
}
questionFile.close();
}
Question Game::getQuestion(int c, int r)
{
return questions[c][r];
}
string Game::getQuestionText(int c, int r)
{
return questions[c][r].question;
}
Note: the cout called in the game method does return exactly what it should!
Question class:
class Question {
public:
int col;
int row;
bool dailyDouble;
string question;
string answer;
int value;
Question();
Question(int, int, bool, string, string);
bool checkAnswer(string);
string getQuestion();
};
Question::Question() { }
Question::Question(int c, int r, bool d,string q, string a)
{
col = c; row = r; dailyDouble = d; question = q, answer = a;
cout << "TEST> Q: " << question << ", A: " << answer << endl;
if(d)
value = r * 200 * 2;
else
value = r * 200;
}
bool Question::checkAnswer(string answer)
{
if(answer.find("What is") && answer.find(answer))
return true;
return false;
}
string Question::getQuestion() {
return question;
}
I really can't understand what's going wrong here, any help is greatly appreciated. I hope that once I figure out what's going wrong here I'll be able to finish on my own!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在我的计算机上尝试了您的代码,似乎 Question 类和 Game 类都是正确的,我还尝试了 Game::getQuestion() 和 Game::getQuestionText() 方法,它都是正确的,并返回正确的价值。
顺便说一句,Question类和Game类中没有指针成员,因此不需要编写复制构造函数和operator=()重载方法。
也许您可以检查您在 g.getQuestion(col,row) 中传递的 col 和 row 是否正确。
抱歉必须在这里发布答案,因为我不知道如何为您的问题添加评论。
希望这会有所帮助。
I tried your code on my computer, it seems that both the Question class and the Game class is correct, I also tried the Game::getQuestion() and the Game::getQuestionText() method, it's all correct, and returns the proper value.
By the way, there are no pointer members in the Question Class and the Game Class, so there is no need to write a copy constructor and a operator=() overload method.
Maybe you can check if the col and row you passed in g.getQuestion(col,row) is right or not.
Sorry have to post an answer here, cause I don't know how to add a comment to your question.
wish this will help.
看起来 Game::getQuestion(int c, int r) 按值返回 Question 类。我认为您不想这样做,很可能您希望“curQuestion”成为一个指针(Question *)并且让 getQuestion 也返回一个指向问题的指针。
这样,问题将存储在游戏类中,并由调用 getQuestion 的任何内容临时引用。
It looks like Game::getQuestion(int c, int r) is returning a Question class by value. I don't think you want to be doing this, most likely you want 'curQuestion' to be a pointer (Question*) and have getQuestion also return a pointer to a Question.
This way the questions will be stored in the game class and just temporally referenced by whatever calls getQuestion.