Thread: linked lists, text files, and classes

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    8

    linked lists, text files, and classes

    ok so i know im asking for homework help but ive tried everything else and still cant seem to find what i need to know..im not nessessarily just asking for the code just for help with a particular part of it....i have a struct inside a class inside another class. i have to read from a text file into a dynamically allocated linked-list. and to sum up all the compilation errors im getting are function calls not matching, function definitions not allowed, and invalid memory addresses.
    my header file looks like this:
    Code:
    //**************************************************************
    //Description:
    //       define class and variables and its parameters.
    //**************************************************************
    
    #ifndef EMPLOYEEDATA_H
    #define EMPLOYEEDATA_H
    
    using namespace std;     //dont know why this is required but  
                             //string wouldnt 
                             //work without it
    
    struct PERSON
    {
        int EmployeeID;
        string EmployeeName;
        string EmployeePhone;
        int EmployeeAge;
    };
    
    
    class EmployeeData
    {
       private:
     
          class ListNode
          {
    
              friend class EmployeeData;
              PERSON person;
              ListNode *next;
              ListNode( PERSON person1, ListNode *next1 = NULL)
              {
                  person = person1;
                  next = next1;
              }
          };
          
          ListNode *head;           //head ptr;
          ListNode *result;         //used to return search;
    
       public:
    
          EmployeeData()
              {  head = NULL; }     //constructor;
    
          void PrintWelcome();
          void FillList(PERSON person, ifstream &inFile);
          void InsertNode(PERSON person);
          void DeleteNode(PERSON person);
          void DisplayList();
          void PrintOne();
          void Update();
          int LinearSearch();
          ~EmployeeData();          //deconstructor;               
    
    };
    
    #endif

    my initialization file looks like this:
    Code:
    //***********************************************************************
    //Description:
    //      function declarations
    //
    //Input:
    //      1.EmployeeData.h
    //      2.iostream
    //***********************************************************************
    
    #include<iostream>   //for cout and NULL
    #include<fstream>    //for file stream
    #include"EmployeeData.h"
    #include<string>
    
    using namespace std;
    
    
    
    //***********************************************************************
    //Function Name:   FillList
    //Desription:
    //      put everything in file into dynamicaly allocated linked lists
    //Input:
    //     1. class EmployeeData
    //     2. iostream
    //***********************************************************************
    void EmployeeData::FillList(PERSON person, ifstream & inFile)
    {
       ListNode *newNode;
       ListNode *nodePtr;
       ListNode *previousNodePtr = NULL;
    
       inFile>>person.EmployeeID
                               >>person.EmployeeName
                               >>person.EmployeePhone
                               >>person.EmployeeAge;
       
       //allocate a new node and store struct values there
       newNode= new ListNode;
       newNode.person->EmployeeID = person.EmployeeID;
       newNode.person->EmployeeName= person.EmployeeName;
       newNode.person->EmployeePhone= person.EmployeePhone;
       newNode.person->EmployeeAge = person.EmployeeAge;
       
       newNode->next = NULL;
    
       while(!inFile.endof)
       {
    
       if(!head)
            head = newNode;
       else
       {
         
           //position nodePtr at the head of the list
           nodePtr = head;
    
           while(nodePtr->next)
    
                  nodePtr = nodePtr->next;
      
           nodePtr->next = newNode;
       }
    
       inFile>>person.EmployeeID
                               >>person.EmployeeName
                               >>person.EmployeePhone
                               >>person.EmployeeAge;
       
       //allocate a new node and store struct values there
       newNode= new ListNode;
       newNode.person->EmployeeID = person.EmployeeID;
       newNode.person->EmployeeName= person.EmployeeName;
       newNode.person->EmployeePhone= person.EmployeePhone;
       newNode.person->EmployeeAge = person.EmployeeAge;
    
       newNode->next = NULL;               
    }
    and my main looks like this:

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    #include<iomanip>
    #include<cstdlib>
    #include"EmployeeData.cpp"
    using namespace std;
    
    //global variables
    const int MAX = 15;
    
    //prototypes
    void PrintWelcome();
    
    
    int main()
    {
       EmployeeData data;           //class
       PERSON person;
       int number;                  //number of nodes
       ifstream inFile;
    
       inFile.open("empdb.data");
       if(inFile.fail())
       {
           cout<<"\n\nError: cannot open file"<<endl<<endl;
           exit(1);
       }
    
       PrintWelcome();   
    
       FillList(person,inFile);
    
    
     // ProcessTrans(students,n);
    
      inFile.close();
    return 0;
    }
    
    //**********************************************************************
    //Description:
    //       print welcome message.
    //**********************************************************************
    void PrintWelcome()
    {
        cout<<"\n\nWELCOME TO THE NEUVELLE CO. EMPLOYEE DATABASE"<<endl<<endl;
    }
    any help would be appreciated.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    At first glance, I noticed this, which it could have something to do with
    Code:
    #include"EmployeeData.cpp"
    You may only include headers.

    Also, post the errors that you get. They say more than just "error" for a reason.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    Code:
    EmployeeData.cpp: in member fxn void EmployeeData::FillList(PERSON, ifstream):
    
    Employeedata.cpp:39: no matching fxn call to
    EmployeeData::listnode::listnode()
    Employeedata.h:31: note: candidates ...(left out because of length)
    
    Employeedata.cpp:40-43: request for member person in new node which is of
    non class type EmployeeData::ListNode*
    
    EmployeeData.cpp:47: (i now whats wrong with this erro)
    
    (fromt eh look of it the above errors repeat for different line number)
    
    in prog2.cpp:38: error a fxn definition is not allowed before {
    
    in prog2.cpp:67: same thing
    in prog2.cpp:69: expected } at end of input
    not actually says some of those but wouldnt let me post with out inserting it as code
    that looks like i

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    okay after changing that .cpp to .h my only error is in prog2.cpp 53..Fill list not declared

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Code:
       FillList(person,inFile);
    needs to be
    Code:
       data.FillList(person,inFile);

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    with changes

    cs125462@classes:~/prog2> g++ prog2.cpp

    /tmp/ccoWOYRB.o: In function `main':
    prog2.cpp.text+0xdc): undefined reference to `EmployeeData::FillList(PERSON, std::basic_ifstream<char, std::char_traits<char> >&)'
    collect2: ld returned 1 exit status

    im sry i dont understand.. now its set up exactly like the book shows and still get this error.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Looks like you are not linking in the .cpp file with the Employee implementation, just compiling prog2.cpp...there are two ways to deal with this:
    1. Add it to your project
    2. If you don't know how to do one a tacky way of doing it (I know I will catch hell from MK for this but you can add the code in Employee.cpp *above* main in prog2.cpp. This will get you beyond your immediate problem but I would highly suggest you learn about linking more than one .cpp file to your code...it is needed in so many ways...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    does anyone know how to in this case or at least a place i can find out how to link them together?

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    OK I teach better by example. Extrapolate what you need for your situation. In this scenario I have three files:
    main.cpp: program entry point. creates instance of a class I have and executes a method.
    libfile.h: header file for external cpp/library
    libfile.cpp: implementation of the class set up in libfile.h

    The files look like this:
    Code:
    // libfile.h, header for library
    
    // use guards always
    #ifndef LIBFILE_H
    #define LIBFILE_H
    
    // class
    class MyClass
    {
      public:
       MyClass() {}
       void showName();
    };
    
    #endif

    Code:
       
    // libfile.cpp: library implementation
    
    #include "libfile.h"
    
    // add in the system includes
    #include <iostream>
    
    // set the namespace
    using namespace std;
    
    // main method in class
    void MyClass::showName()
    {
       cout << "MyClass" << endl;
    }
    and finally the test-harness/program entry point, main:
    Code:
    // main file
    
    // include the library/external cpp file
    #include "libfile.h"
    
    
    // program entry point
    int main(int argc, char *argv[])
    {
       // set up the result code
       int nRC = 0;
    
       // create instance of class
       MyClass Instance;
    
       // call method
       Instance.showName();
    
       // end main, return result
       return nRC;
    }
    Now to compile all of the .cpps and link them together:
    Code:
    jeff@jeff-gate:~/dev/testgcc$ g++ main.cpp libfile.cpp -o testapp
    jeff@jeff-gate:~/dev/testgcc$ ls
    libfile.cpp  libfile.h  main.cpp  testapp
    jeff@jeff-gate:~/dev/testgcc$ ./testapp
    MyClass
    jeff@jeff-gate:~/dev/testgcc$
    Any questions?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    okay that makes a little more since but can i ask where the testapp came from?

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Look closely at the compile line:
    Code:
    jeff@jeff-gate:~/dev/testgcc$ g++ main.cpp libfile.cpp -o testapp
    That is telling G++ to compile main.cpp and libfile.cpp and -o (output) them to an executable called 'testapp'. IOW this is the name that your resulting executable could be. If you wanted the app to be called MyFavoriteHackingTool you would have replaced the -o statement with -o MyFavoriteHackingTool.

    IOW testapp was generated by the compiling/linking process. I just did it to create a test to illustrate that the second file in indeed get linked since it was called by the first file...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone with a spare java class file(s) with nested classes?
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-29-2009, 08:33 PM
  2. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  3. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 12:37 AM
  4. Linked Lists and Inheritance
    By mattyneedshelp! in forum C++ Programming
    Replies: 6
    Last Post: 02-01-2003, 07:47 PM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM