[英]Undefined reference to?
我正在用C ++為骰子傻瓜游戲編寫代碼,但是我一直遇到錯誤代碼“對'cootieComplete(int)'的未定義引用”,我不確定如何解決它。 任務是創建一個傻瓜游戲,在其中擲骰子,然后應用它以將不同的身體部位添加到該傻瓜中。
這是我的代碼。
#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);
如果您需要它們,Eric是正確的。 使它們看起來像
bool cootieComplete(int, int, int, int, int, int);
bool cootieComplete(int numBody,int numHead,int numAntenna,int numWings,int numTongue,int numLeg){
// Code here
}
那應該解決你的錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.