2014-02-08 75 views
0

好吧,可能是一個愚蠢的問題給你們,但我無法弄清楚。if語句在主函數C++

所以我在課堂上參加了一個C++基礎課程,到目前爲止我還在努力/哭泣。

我不能告訴你們我的代碼,因爲我不被允許/如果我被發現會有後果,但我可以舉個例子。

我正在使用xcode。所以,當我編譯時,我得到兩個錯誤(圖像提供)。 我搜索了類似的問題,但這些問題與我正在做的相比似乎太複雜了。此外,唯一包括我有iostream和字符串。

我知道當我在主函數中添加if語句時會發生問題。我知道這一點,因爲當我刪除它時,一切都按預期編譯。但是當我再次將它添加到主函數時,會發生這些錯誤。

所以我的問題是,基於我所知道的是,在主函數中添加if語句是否合適?

下面是一個例子。我寫了下面的函數並在上面調用。

#include <iostream> 
#include <string> 

using namespace std; 

// example functions that I just made up to explain the structure of my actual code. 

//Don't bother trying to understand it. It's just to explain that 
//I wrote my functions at the 
// bottom and called it at the top. 

int getNumberofWins(param1, param2); 

string getTheName(int player1); 

int executeCycle(string p1_name, string p2_name); 

void stateWinner(string winner_name); 


int main { 

playerOne = getTheName(1); 
playerTwo = getTheName(2); 


r1 = executeCycle(playerOne, playerTwo); 



r2= executeCycle(playerOne, playerTwo); 

totalWin1 = getNumberOfWins(1, r1, r2); 
totalWin2 = getNumberOfWins(2, r1, r2); 

cout << totalWin1; 

//This is the where I get the errors. When I delete the if statement, 
//Everything compiles. When I add it, an error occurs. 

if (totalWin1 == 2){ 
    stateWinner(playerOne); 
} 
return 0; 
} 

string getTheName(int player1){ 

string playerOne; 
string playerTwo; 

if(player_number == 1){ code code code 
} 

} 

int getNumberofWins (int param1, int param2){ 
code code code 
} 

int executeCycle(string p1_name, string p2_name){ 
    code code code 
} 

void stateWinner(string winner_name){ 
if(!winner_name.empty()){ 
    code code code 
} 

我希望沒關係,如果上面的代碼不準確。我認爲的一點是,一旦我將if語句添加到主函數中,就會顯示出兩個錯誤。

enter image description here

enter image description here

居然......現在,我看它,他們都看起來像類似的錯誤。我只是不知道爲什麼他們都出現...

對不起,如果這是一個明顯的答案,或者如果它不明確。

+2

'win'是如何定義的以及在哪裏? – herohuyongtao

+0

沒有'stateWinner'函數聲明。 – Barmar

+0

錯誤消息顯示'announceWinner' - 我在代碼中沒有看到任何提及,所以目前還不清楚爲什麼會發生這種情況。如果錯誤抱怨'stateWinner'會更有意義。 – Barmar

回答

0

的 「announceWinner」 功能沒有在任何地方定義,即沒有

void announceWinner() { 
    // code 
} 

任何地方。要麼你還沒有寫入它,或者包含它的文件沒有被編譯爲與主程序鏈接的&。