未定义的引用
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
}
这应该修复你的错误
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中