c++ - undefined reference ?

标签 c++

我正在用 C++ 编写掷骰子 cootie 游戏的代码,但我一直遇到错误代码“未定义对‘cootieComplete(int)’的引用”,而且我不确定如何修复它。 作业是创建一个虱子游戏,您可以在其中掷骰子,然后应用它为虱子添加不同的 body 部位。

这是我的代码。

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

     }

最佳答案

声明 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);

关于c++ - undefined reference ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22495060/

相关文章:

c++ - 查找包含优于前向声明的用例

python - boost需要特殊的python模块才能在linux中构建吗

c++ - Protocol Buffer : Understanding the compiled output of proto files

XML 库的 C++ 'wrapper class'

c++ - Boost Asio GCC 链接错误

c++ - 如何将迭代器变量声明为私有(private)成员变量?

c++ - 插入元素后 vector 的容量增加?

c++ - 如何使用库组织 QtCreator 项目

c++ - c++程序中的c文件

c++ - mac 上 gdb 7.6 断言缺少调用堆栈帧