2011-05-18 66 views
1

我一直在這個項目上工作了一段時間,這對我來說是一種新的語言,但我有一個經驗豐富的合作伙伴,他有點不屑於我。無論如何,現在我無法將文本從一個對象轉移到另一個對象。我在遊戲類中實例化一個對象,然後嘗試獲取它並將其保存到主類中的另一個對象,但是當我得到該對象時它是空的!我不知道這裏發生了什麼,我似乎無法弄清楚。值傳遞不正確?

,但不工作的部分是在顯示方法,當我嘗試繪製文本的問題:

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; 
} 

注:所謂的T中的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; 
} 

我實在不明白這是怎麼回事錯在這裏,任何幫助是極大的讚賞。我希望一旦我弄清楚發生了什麼問題,我就可以自己完成!

+1

Way.Too.Much.Code。永遠記住[this](http://sscce.org)。 – Xeo 2011-05-18 00:49:32

+0

對不起,我不確定是否有人試圖幫助將需要所有..想要在安全的一面。我會砍掉它。 – CaffeinatedCM 2011-05-18 00:54:34

+0

@Java:我們只需要知道錯誤的地方。其他一切只會分散注意力。 :) – Xeo 2011-05-18 00:58:08

回答

0

我在我的電腦上試過你的代碼,似乎Question類和Game類都是正確的,我也嘗試了Game :: getQuestion()和Game :: getQuestionText()方法,它都是正確的,並返回適當的值。

順便說一下,Question Class和Game Class中沒有指針成員,所以不需要寫一個拷貝構造函數和一個operator =()重載方法。

也許你可以檢查你在g.getQuestion(col,row)中傳遞的列和行是否正確。

對不起,要在這裏發表一個答案,因爲我不知道如何添加評論到您的問題。

希望這會有所幫助。

+0

好的..我仍然無法正常工作:/這讓人非常沮喪。我再次檢查了它,它給出了正確的列和行 – CaffeinatedCM 2011-05-18 02:27:16

+0

@ JavaJosh94你可以給我你的整個解決方案嗎?我很好奇。我的電子郵件地址是:126.com – shengy 2011-05-18 03:18:08

+0

對不起,我剛剛回來並注意到了這一點。我完全拿出遊戲類和問題類,然後把一對夫婦的數組放到存儲問題和答案的主類中。這不是很有效,但它的工作:P – CaffeinatedCM 2011-05-27 14:02:32

0

它看起來像Game :: getQuestion(int c,int r)按值返回一個Question類。我認爲你不想這樣做,很可能你想'curQuestion'是一個指針(Question *),並且getQuestion也返回一個指向Question的指針。

這樣,問題將存儲在遊戲類中,並且只需通過任何調用getQuestion進行暫時引用即可。