2

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

Deanie
  • 2,316
  • 2
  • 19
  • 35
AdamY
  • 851
  • 2
  • 9
  • 20
  • Which C++ compiler are you using? –  Jul 06 '10 at 14:59
  • 3
    You should add the C tag. This is hardly C++ besides the included headers and `using` clause. – pmr Jul 06 '10 at 15:10
  • @AdamY Don't use DevC++ - it is incredibly buggy and not being developed any more - try http://www.codeblocks.org. And as for your specific problem - ASK YOUR TEACHER - why do so many students find this so difficult to do? –  Jul 06 '10 at 15:27
  • 1
    @Neil Butterworth Thank you for the suggestion, I'll look into codeblocks. I was using DevC++ only because I tried teaching myself C/C++ a little while ago out of C++ for Dummies, and they suggested DevC++ (I guess I am the dummy. :/) Also, the reason more students don't just ask the teacher is because more students are irresponsible and stubborn like myself and start an assignment the day before it is due. – AdamY Jul 06 '10 at 15:33
  • Things like `{{` makes my head hurt. Feel free to use newlines more. – Evan Teran Jul 06 '10 at 15:45
  • @Neil Butterworth I just installed CodeBlocks and it's already much better than DevC++, thanks again. – AdamY Jul 06 '10 at 15:46
  • please could someone edit the title of the question. the problem has nothing to do with linked lists. it is just a plain linker error. Thanks – Jens Gustedt Jul 06 '10 at 16:13
  • apart from including stdio twice the C way first and the C++ way secondly, it can be considered C++ code... using none of the C++ feature... - namespace apart, which however can be dropped (altogether with the iostream include)... – ShinTakezou Jul 06 '10 at 17:20

5 Answers5

5

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
  • 43,959
  • 6
  • 69
  • 99
  • 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 Jul 06 '10 at 15:29
  • 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 Jul 06 '10 at 15:35
  • 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 Jul 06 '10 at 15:41
  • Also, thank you for providing a pseudo code example. It's always 100 times more helpful to actually see the template. – AdamY Jul 06 '10 at 15:42
  • 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 Jul 06 '10 at 15:47
  • This should be enough to get me going again. Thank you very much. – AdamY Jul 06 '10 at 15:47
2

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
  • 59,039
  • 12
  • 119
  • 163
  • 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 Jul 06 '10 at 15:09
  • 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 Jul 06 '10 at 15:16
  • 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 Jul 06 '10 at 15:21
  • read about file I/O in C/C++. just google "reading data from file c++" – Andrey Jul 06 '10 at 15:30
0

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
  • 69,361
  • 7
  • 100
  • 138
  • 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 Jul 06 '10 at 15:10
  • 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 Jul 06 '10 at 15:14
  • 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 Jul 06 '10 at 15:25
  • 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 Jul 06 '10 at 15:58
0

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
  • 5,539
  • 2
  • 22
  • 26
0

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
  • 10,450
  • 1
  • 37
  • 50
  • 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 Jul 06 '10 at 15:22