简体   繁体   中英

C++ Linker Error

I'm new to C++ and was working on an assignment for a class. We were given a .txt file, have to read information from it, and store it in a linked list, and then print it out to the user. After hours of trying to manipulate the examples we were given, and another couple hours of trying to write the code from scratch, I'm getting closest with a little of both.

The file is called payroll.txt and has about 30 or so lines in this type of format:
Clark Kent 55000 2500 0.07
Lois Lane 65000 1000 0.06
Tony Stark 70000 1500 0.05

Our professor is really big on commenting on our code, so I hope it helps. This is my code:

#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 ); 
} 

Now when I compile, I get the errors:
[Linker error] undefined reference to ReadRecord()
[Linker error] undefined reference to PrintRecord(employeeType*)

I'm almost sure that the ReadRecord and PrintRecord commands he gave us in the example are Pseudo Code meant to mess us up, but I have no idea what could go there. I've been pouring over multiple textbooks and searching for a simple way to fix linker errors online, and have run out of ideas.

If anyone could help me out/point me in the right direction it would be greatly appreciated. A link to a webpage with more info on linked lists and Linker Errors would be even more awesome.

Thanks,
Adam

The linker is complaining that you have referenced the functions ReadRecord and PrintRecord , but you haven't written them yet. You can write these functions at the end of the current file. You can start with this template:

// 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;
}

With these function templates added to the file, the linker should no longer complain about undefined references (since the functions have now been created). However, the code won't work correctly since these functions don't actually do anything. You will need to fill in the body of the functions (based on the details of your assignment).

Edit: I have included a few hints (as code comments) in case you don't know where to begin. For detailed help on parsing data from a text file or displaying information to the screen, consult your textbook (it should have many examples that would help you in this instance).

Update: A few links:

for those functions you have only prototypes:

Employ *ReadRecord(); 
void PrintRecord(Employ *);

but no bodies. So linker can't find them. Did you forget to add another file with body of those functions?

You probably were given a header file (.h) file but there is no ReadRecord(...) or PrintRecord(...) functions defined in it's corresponding source code (.cpp, .cc, .cxx) file. Either that, or you failed to compile the .c file so there's no .o file for your linker to include.

The two functions you are trying to use, ReadRecord() and PrintRecord(Employ *) , have not yet been defined. You will no longer get these Linker errors once you define these functions.

Judging from the way you have used the functions, ReadRecord is meant to read the file, create an Employ from the information read, and return it. PrintRecord is meant to print out the information contained in an Employ (probably printed in a format provided to you by your professor).

I hope that helps.

All you need is just implement ReadRecord() and PrintRecord() functions. Apparently, ReadRecord() should read records from file, using file descriptor or file name as input argument and PrintRecord() should print to stdout or file of the name given as input argument. Anyway, the details are your design specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM