我从事这个项目已有一段时间了,这对我来说是一门新语言,但我有一个经验丰富的合作伙伴有点抛弃了我。无论如何,现在我无法将文本从一个对象获取到另一个对象。我在游戏类中实例化一个对象,然后尝试获取它并将其保存到主类中的另一个对象,但是当我获取该对象时它是空的!我不知道这里发生了什么,而且我似乎无法弄明白。
但是当我尝试绘制问题文本时,不工作的部分在显示方法中:
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;
}
我真的不明白这里出了什么问题,非常感谢任何帮助。我希望一旦我弄清楚这里出了什么问题,我就能自己完成!
最佳答案
我在我的电脑上试了你的代码,似乎Question类和Game类都是正确的,我也试了Game::getQuestion()和Game::getQuestionText()方法,都是正确的,并且返回正确的值。
对了,Question类和Game类中没有指针成员,所以不需要写拷贝构造函数和operator=()重载方法。
也许你可以检查你在 g.getQuestion(col,row) 中传递的 col 和 row 是否正确。
抱歉必须在这里发布答案,因为我不知道如何为您的问题添加评论。
希望这会有所帮助。
关于c++ - 值没有正确传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6038727/