2014-03-19 122 views
1

我正在写C++的代码掷骰子cootie游戏,但我一直运行到错误代码“未定义引用'cootieComplete(int)'”&我不知道如何解决它。 这个任务是创建一个cootie游戏,在其中掷骰子然后将其应用于向cootie添加不同的身体部位。未定义的引用?

这是我的代码。

#include <iostream> 
#include <cstdlib> 
#include <time.h> 

using namespace std; 
int rolldice(); 
int applyroll(int); 
bool cootieComplete(int); 
void printCootie(int); 

     int numBody=0; 
     int numHead=0; 
     int numAntenna=0; 
     int numWings=0; 
     int numTongue=0; 
     int numLeg=0; 

int main(int argc, char *argv[]) 
{ 
    int diceroll; 
    int apply; 
    srand(time(0)); 
    do { 
    diceroll = rolldice(); 
    apply = applyroll(diceroll); 
    printCootie(diceroll); 
    } 
    while (!cootieComplete(apply)) ; 


    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

    int rolldice() 
{ 
    int diceroll = rand() % 6 + 1; 
    return diceroll; 
} 

    int applyroll(int diceroll){ 

        if (diceroll == 1) 
        { 
        numBody++; 
        } 

        if (diceroll == 2 && numBody >= 1) 
        { 
        numHead++; 
        } 

        if (diceroll == 3 && numHead >=1) 
        { 
        numAntenna++; 
        } 

        if (diceroll == 4 && numBody >= 1) 
        { 
        numWings++; 
        } 

        if (diceroll == 5 && numHead >= 1) 
        { 
        numTongue++; 
        } 

        if (diceroll == 6 && numBody >= 1) 
        { 
        numLeg++; 
        } 
      return diceroll; 
      } 

    bool cootieComplete(int numBody,int numHead,int numAntenna,int numWings,int numTongue,int numLeg) { 
     if (numBody>0&&numHead>0&&numAntenna>1&&numWings>1&&numTongue!=0&&numLeg>=4) { 
      return true; 
      } 
     else { 
       return false; 
      } 
    } 

    void printCootie(int diceroll){ 

        if (diceroll == 1) 
        { 
        cout << "Body Added!" << endl; 
        cout << "There are " << numBody << " body, " << numHead << " head, " << numAntenna << " antenna, " << numWings << " wings, " << numTongue << " tongues, " << numLeg << " legs." << endl; 
        } 

        if (diceroll == 2 && numBody >= 1) 
        { 
        cout << "Head Added!" << endl; 
        cout << "There are " << numBody << " body, " << numHead << " head, " << numAntenna << " antenna, " << numWings << " wings, " << numTongue << " tongues, " << numLeg << " legs." << endl; 
        } 
        else if (diceroll == 2) 
        { 
        cout <<"No body, you can't add head!"; 
        } 

        if (diceroll == 3 && numHead >=1) 
        { 
        cout << "Antenna Added!"<<endl; 
        cout << "There are " << numBody << " body, " << numHead << " head, " << numAntenna << " antenna, " << numWings << " wings, " << numTongue << " tongues, " << numLeg << " legs." << endl; 
        } 
        else if (diceroll == 3) 
        { 
        cout << "No head, you can't add antenna!"; 
        } 

        if (diceroll == 4 && numBody >= 1) 
        { 
        cout<<"Wing Added!" << endl; 
        cout << "There are " << numBody << " body, " << numHead << " head, " << numAntenna << " antenna, " << numWings << " wings, " << numTongue << " tongues, " << numLeg << " legs." << endl; 
        } 
        else if (diceroll == 4) 
        { 
        cout << "No body, you can't add wings!"; 
        } 

        if (diceroll == 5 && numHead >= 1) 
        { 
        cout<<"Tongue Added!" << endl; 
        cout << "There are " << numBody << " body, " << numHead << " head, " << numAntenna << " antenna, " << numWings << " wings, " << numTongue << " tongues, " << numLeg << " legs." << endl; 
        } 
        else if (diceroll == 5) 
        { 
        cout << "No head, you can't add tongue!"; 
        } 

        if (diceroll == 6 && numBody >= 1) 
        { 
        cout<<"Leg Added!" << endl; 
        cout << "There are " << numBody << " body, " << numHead << " head, " << numAntenna << " antenna, " << numWings << " wings, " << numTongue << " tongues, " << numLeg << " legs." << endl; 
        } 
        else if (diceroll == 6) 
        { 
        cout << "No body, you can't add legs"; 
        } 

    } 
+0

相信我,你真的不希望/需要的全局变量。 – chris

回答

6

声明 cootieComplete取1个int和你定义其采取6然后调用版本服用1

// Declaration 
bool cootieComplete(int); 

... 

while (!cootieComplete(apply)) 

.... 

// Definition 
bool cootieComplete(int numBody,int numHead,int numAntenna,int numWings,int numTongue,int numLeg) 

既然你叫的版本以1个INT作为参数,编译器正在寻找cootieComplete(int)的定义,但找不到它,所以抛出undefined reference错误。

他们需要匹配。所以定义如下:

bool cootieComplete(int, int, int, int, int, int); 
1

埃里克是正确的,如果你需要他们匹配。使它们看起来像

bool cootieComplete(int, int, int, int, int, int); 

bool cootieComplete(int numBody,int numHead,int numAntenna,int numWings,int numTongue,int numLeg){ 
    // Code here 
} 

这应该改正错误

+0

它仍然抛出错误“功能太少的参数”bool cootieComplete(int,int,int,int,int,int)' – user3060043

+0

@ user3060043你看了Eric所有的东西吗? – David

+0

是的,我改变了它,所以它们匹配 – user3060043