views:

134

answers:

5
+2  Q: 

C++ Linker Error

Hello,

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

+2  A: 

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?

Andrey
There was no other file I received. The professor hasn't gone into separate files, in terms of headers, in the class yet. I guess this means I need to build the bodies.Would you know a site that might have more info on what I'll need to build?
AdamY
probably. `ReadRecord` will read record from file and create new `Employ` instance and return pointer to it. `PrintRecord` will print given record. i don't know what do you want to learn actually.
Andrey
If you know any site that could help me build ReadRecord and PrintRecord, it would be useful. I find myself stuck on understanding the way the information is stored, so I have trouble writing the proper code to read and manipulate it.
AdamY
read about file I/O in C/C++. just google "reading data from file c++"
Andrey
A: 

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.

Edwin Buck
I wasn't given any other file with the example code. What do you mean I may have failed to compile the .c file? (Sorry, I'm still really new to C/C++. I use the Dev-C++ editing tool, and just hit the compile button when I think my code is ready and wait for errors.)
AdamY
AdamY, if you weren't given the .c file with the contents of ReadRecord(...) and PrintRecord(...) then you professor probably meant for you to write those methods. Considering that you probably wrote the "struct employeeType", it would have been impossible for your professor to guess the right struct fields to make these two needed functions work.
Edwin Buck
That is correct. I wrote the "struct employeeType", so he couldn't have predicted the right fields. Where I'm stuck is understanding how the program stores the information, so I don't really know how to go about writing the methods for ReadRecord and PrintRecord.Are there any online sources like cplusplus.com that might have more information on the subject?
AdamY
The program stores the information in the file he gave you, "payroll.txt". You have to read it from the file and populate the record in ReadRecord(...). You have to read your struct and print it as he wishes in PrintRecord(...). Odds are he mentioned what each number meant in class.
Edwin Buck
A: 

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.

Jake Greene
A: 

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.

Pmod
What does it mean exactly to implement the funtions? (Sorry, I'm still very new to C and have only been taking the class online for two weeks.)
AdamY
+4  A: 

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:

bta
Oh... Ok.So the reason I got linker errors is because I needed to properly define the functions? That makes sense. Thank you for clearing up the linker error problem.I've been reading some text books on how to manipulate the code once it's stored, but still don't understand enough about the way data is stored in this program. Do you know of any online sources of information where I can read up on this more?
AdamY
What do you mean when you say that you "don't understand the way data is stored in this program"? Do you mean that you don't understand the way that the data is written in the text file? Or that you don't understand how your program should store the data once it is read from the file?
bta
I mean that I don't understand how the program should store the data and I don't understand how I should reference it. Each time I read about pointers, arrays, and structures it gets a little clearer, but I'm still somewhat confused.Is there a book you can recommend?
AdamY
Also, thank you for providing a pseudo code example. It's always 100 times more helpful to actually see the template.
AdamY
For starters, use the textbook from your class. Your instructor shouldn't be forcing you to dig around for your own resources, everything you need to complete your assignments should be in your class notes or your course's textbook. You may also want to look at http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list and http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list.
bta
This should be enough to get me going again. Thank you very much.
AdamY