C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-06-2010, 05:21 PM   #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.
mbarbay is offline   Reply With Quote
Old 02-06-2010, 06:02 PM   #2
Unregistered User
 
Yarin's Avatar
 
Join Date: Jul 2007
Posts: 1,016
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.
__________________
"Those who would give up Essential Liberty to purchase a little Temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin
Yarin is offline   Reply With Quote
Old 02-06-2010, 06:05 PM   #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
mbarbay is offline   Reply With Quote
Old 02-06-2010, 06:10 PM   #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
mbarbay is offline   Reply With Quote
Old 02-06-2010, 08:28 PM   #5
Unregistered User
 
Yarin's Avatar
 
Join Date: Jul 2007
Posts: 1,016
Code:
   FillList(person,inFile);
needs to be
Code:
   data.FillList(person,inFile);
__________________
"Those who would give up Essential Liberty to purchase a little Temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin
Yarin is offline   Reply With Quote
Old 02-07-2010, 11:26 AM   #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.
mbarbay is offline   Reply With Quote
Old 02-07-2010, 02:34 PM   #7
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
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
jeffcobb is offline   Reply With Quote
Old 02-07-2010, 03:12 PM   #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?
mbarbay is offline   Reply With Quote
Old 02-07-2010, 03:37 PM   #9
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
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
jeffcobb is offline   Reply With Quote
Old 02-09-2010, 10:42 AM   #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?
mbarbay is offline   Reply With Quote
Old 02-09-2010, 10:49 AM   #11
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 887
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
jeffcobb is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Anyone with a spare java class file(s) with nested classes? Sebastiani General Discussions 6 04-29-2009 08:33 PM
classes and header files Drake C++ Programming 8 11-30-2006 07:12 PM
Class files including other classes combatdave C++ Programming 7 11-04-2006 12:37 AM
Linked Lists and Inheritance mattyneedshelp! C++ Programming 6 02-01-2003 07:47 PM
Sharing a variable between classes of different .CPP files divingcrab C++ Programming 5 07-07-2002 02:57 PM


All times are GMT -6. The time now is 12:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22