if函数c ++中的if语句

时间:2014-02-08 01:58:00

标签: c++ xcode if-statement

好吧,对你们来说可能是一个愚蠢的问题,但我无法理解。

所以我在课堂上学习c ++基础课程,到目前为止,我正在努力/哭泣。

我无法向你们展示我的代码,因为我不被允许/如果我被抓住会有后果,但我可能会举一个例子。

我正在使用xcode。因此,当我编译时,我得到两个错误(提供图像)。 我搜索了类似的问题,但与我正在做的相比,这些问题似乎过于复杂。另外,我唯一包括iostream和string。

我知道当我在main函数中添加if语句时会出现问题。我知道这一点,因为当我删除它时,一切都按预期编译。然而,当我再次将它添加到main函数时,会发生这些错误。

所以我的问题是,根据我所知,在主函数中添加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语句添加到main函数,就会出现两个错误。

enter image description here

enter image description here

实际上......现在我看着它们,它们看起来都像是类似的错误。我只是不知道为什么他们都出现......

很抱歉,如果这是一个明显的答案,或者不清楚。

1 个答案:

答案 0 :(得分:0)

“announceWinner”函数未在任何地方定义,即没有

void announceWinner () {
    // code
}

的任何地方。要么你尚未编写它,要么包含它的文件没有编译和&amp;与主程序相关联。