我编写了一个小程序,接受用户输入并创建链接列表。我在构建时遇到一个错误,似乎与HeadPointer传递到print函数有关。错误如下:
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;
}
};
在任何人提起之前,这不是家庭作业。这是我的教授建议我们独立创作以准备未来的中期任务。
谢谢!
答案 0 :(得分:2)
函数声明和定义不匹配。
该功能的原型是
void PrintList(PetData*);
,定义为void PrintList()
。这就是它不匹配的原因。
有更正后的代码
void PrintList(PetData* HeadPointer){
while(HeadPointer != NULL){
cout << HeadPointer->PetType << "\t" << HeadPointer->IdNumber << endl;
HeadPointer = HeadPointer->link;
}