我编写了一个小程序,它接受用户输入并创建一个链表。我遇到了一个错误,当建筑似乎涉及到传递到打印函数的头指针。错误如下:
undefined reference to `PrintList(PetData*)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
如果有人可以指出(不是有意的)我在正确的方向,甚至明确地给我一个修复,我将非常感激。源代码:
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
struct PetData{
int IdNumber;
string PetType;
PetData *link;
};
PetData * HeadPointer=NULL;
void InsertItem(int, string, PetData*);
void PrintList(PetData*);
int main(){
int init_IdNumber;
string init_PetType;
PetData * CRP = NULL;
char cflag = 'Y';
while (toupper(cflag) == 'Y'){
cout << "Please Enter an ID Number: " << endl;
cin >> init_IdNumber;
cout << "Please Enter a Pet Type: " << endl;
cin >> init_PetType;
CRP = new PetData;
InsertItem(init_IdNumber,init_PetType,CRP);
HeadPointer=CRP;
cout << "Would you like to enter another? (Y/N)" << endl;
cin >> cflag;
};
PrintList(HeadPointer);
return 1;
};
void InsertItem(int init_IdNumber, string init_PetType, PetData* CRP){
CRP->PetType = init_PetType;
CRP->IdNumber = init_IdNumber;
CRP->link = HeadPointer;
};
void PrintList(){
while(HeadPointer != NULL){
cout << HeadPointer->PetType << "t" << HeadPointer->IdNumber << endl;
HeadPointer = HeadPointer->link;
}
};
在任何人提到它之前,这不是家庭作业。这是我的教授建议我们独立创作的东西,为将来的期中作业做准备。
谢谢!
函数声明和定义不匹配
函数的原型为void PrintList(PetData*);
,定义是void PrintList()
。这就是不匹配的原因。
有正确的代码
void PrintList(PetData* HeadPointer){
while(HeadPointer != NULL){
cout << HeadPointer->PetType << "t" << HeadPointer->IdNumber << endl;
HeadPointer = HeadPointer->link;
}