[英]C++ Linker Error
我是C ++的新手,正在为课程做作业。 我们获得了一个.txt文件,必须从其中读取信息,并将其存储在链接列表中,然后将其打印出给用户。 在经过数小时的尝试来操作示例之后,再经过数小时的尝试从头开始编写代码,我对两者的了解越来越近了。
该文件称为payroll.txt,这种格式的文件大约有30行:
克拉克·肯特55000 2500 0.07
路易斯巷65000 1000 0.06
托尼·史塔克70000 1500 0.05
我们的教授非常重视评论我们的代码,因此希望对您有所帮助。 这是我的代码:
#include <cstdlib>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAX_STR 100
/* Structure Definition */
typedef struct employeeType Employ;
struct employeeType {
char first[MAX_STR]; /* first name */
char last[MAX_STR]; /* last name */
int salary; /* salary */
int bonus; /* bonus */
double deduc; /* percent deduction */
Employ *next;
};
/* operations on the data */
Employ *ReadRecord();
void PrintRecord(Employ *);
main()
{{
Employ *head, *tail, *newp, *tmp;
head = tail = newp = tmp = NULL;
FILE *in; /* file description */
/* open a file, check if it's there */
if( (in = fopen( "payroll.txt", "r" )) == NULL )
{
printf( "Error opening file\n" );
exit(1);
}
while( newp = ReadRecord() )
{
/* Add object to the list */
if( head == NULL )
{
/* Beginning of the list */
head = newp;
/* Current record */
tail = newp;
}
else
{
/* Previous record reference to new record */
tail->next = newp;
/* Current record */
tail = newp;
}
}
/* End of the list */
tail->next = NULL;
/* Loop through the list */
for( tmp=head; tmp!=NULL; tmp=tmp->next )
{
PrintRecord( tmp );
}
现在,当我编译时,我得到了错误:
[链接器错误]对ReadRecord()的未定义引用
[链接器错误]未定义对PrintRecord(employeeType *)的引用
我几乎可以确定他在示例中提供给我们的ReadRecord和PrintRecord命令是伪代码,旨在使我们搞砸,但我不知道该怎么办。 我一直在翻阅多本教科书,并寻找一种简单的方法来在线修复链接器错误,并且用尽了所有的想法。
如果有人可以帮助我/指出正确的方向,将不胜感激。 链接到网页的链接列表和链接器错误的详细信息会更加出色。
谢谢,
亚当
链接器抱怨您已引用函数ReadRecord
和PrintRecord
,但尚未编写它们。 您可以在当前文件的末尾编写这些函数。 您可以从以下模板开始:
// Read a record from the file and parse the data into a structure
Employ *ReadRecord (void) {
// Use fgets() to read a line from the file
// Create a new Employ object to hold the data
// Use sscanf() to parse individual fields out of the string
// and store them in the new Employ object
// Return a pointer to the new Employ object
return (Employ*)NULL;
}
// Print the information from the structure to the screen
void PrintRecord (Employ *ptr) {
// Use printf() to display the content of each field
return;
}
将这些功能模板添加到文件后,链接器将不再抱怨未定义的引用(因为现在已经创建了功能)。 但是,由于这些函数实际上没有执行任何操作,因此代码无法正常工作。 您将需要填写函数的主体(基于分配的详细信息)。
编辑:如果您不知道从哪里开始,我已经包含了一些提示(作为代码注释)。 有关解析文本文件中的数据或在屏幕上显示信息的详细帮助,请查阅您的教科书(在这种情况下,它应该有许多示例对您有所帮助)。
更新:一些链接:
对于这些功能,您只有原型:
Employ *ReadRecord();
void PrintRecord(Employ *);
但没有尸体。 因此,链接器找不到它们。 您是否忘记添加带有这些功能主体的另一个文件?
您可能已经获得了头文件(.h)文件,但是在其相应的源代码(.cpp,.cc,.cxx)文件中没有定义ReadRecord(...)或PrintRecord(...)函数。 要么如此,否则您无法编译.c文件,因此链接器没有要包含的.o文件。
您尝试使用的两个函数ReadRecord()
和PrintRecord(Employ *)
尚未定义。 定义这些函数后,您将不再遇到这些Linker错误。
从您使用功能的方式来看, ReadRecord
意味着读取该文件,创建一个Employ
从信息读取,并将其返回。 PrintRecord
是为了打印出包含在信息Employ
(可能是印在你的教授提供给您的格式)。
希望对您有所帮助。
您只需要实现ReadRecord()
和PrintRecord()
函数即可。 显然, ReadRecord()
应该使用文件描述符或文件名作为输入参数从文件读取记录,而PrintRecord()
应该打印到stdout或具有输入参数名称的文件。 无论如何,细节都是您的设计特定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.