![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 8
| linked lists, text files, and classes 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;
}
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;
}
|
| mbarbay is offline | |
| | #2 |
| Unregistered User 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" 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 | |
| | #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
that looks like i |
| mbarbay is offline | |
| | #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 | |
| | #5 |
| Unregistered User Join Date: Jul 2007
Posts: 1,016
| Code: FillList(person,inFile); 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 | |
| | #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 | |
| | #7 |
| Registered User 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... |
| jeffcobb is offline | |
| | #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 | |
| | #9 |
| Registered User 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;
}
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;
}
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$ |
| jeffcobb is offline | |
| | #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 | |
| | #11 |
| Registered User 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 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... |
| jeffcobb is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |