Problem declaring class object array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Problem declaring class object array

 
0
  #1
Jan 26th, 2008
Hey folks - i am working on an assignment for school and would like some input if possible.

I am trying to declare a class object in the form of an array so that i can read info from file and then manipulate the data.

I need 3 seperate c++ files for this assignment. One is a header file, which contains the class definition etc. The other cpp file contains the class methods, while the third contains the main part of the program. Both the main cpp file and the methods cpp file have the header file included
  1. #include "Employee.h"

Within the main.cpp file i am trying to do this:
  1. #include "Employee.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int buildEmployeeList(Employee []);
  7.  
  8. int main()
  9. {
  10. int array_size = 0;
  11. // Declare Employee object array for employees
  12. Employee employee_list[100];
  13.  
  14. array_size = buildEmployeeList(employee_list); //Builds the employee array object
  15.  
  16. system("PAUSE");
  17. return 0;
  18. }
  19.  
  20. int buildEmployeeList(Employee employee_list[])
  21. {
  22. //DOES STUFF
  23. int count = 1;
  24. return count;
  25. }

I get a linker error with the above code. My header file is:

  1. #ifndef EMPLOYEE_H
  2. #define EMPLOYEE_H
  3.  
  4. #include <fstream>
  5. #include <istream>
  6.  
  7. using namespace std;
  8.  
  9. class Employee
  10. {
  11. private:
  12. char firstName[11];
  13. char lastName[16];
  14. char department[16];
  15. double salary;
  16. public:
  17. Employee();
  18. char *getDepartment();
  19. double getSalary();
  20. void print();
  21.  
  22. };
  23.  
  24. #endif /* EMPLOYEE_H */

And my method cpp file is:

  1. #include "Employee.h"
  2. #include <iostream>
  3.  
  4. /****************************************************************
  5.   FUNCTION: Employee:Employee()
  6.   ARGUMENTS: none
  7.   RETURNS: none
  8.   NOTES: This is the class Employee constructor
  9. ****************************************************************/
  10. Employee::Employee()
  11. {
  12. firstName[0] = '\0'; //Sets null char to firstName
  13. lastName[0] = '\0'; //Sets null char to lastName
  14. salary = 0; //Sets salary to 0
  15. }
  16.  
  17. /****************************************************************
  18.   FUNCTION: Employee::getDepartment()
  19.   ARGUMENTS: none
  20.   RETURNS: Department name
  21.   NOTES: Returns the name of the department for the individual
  22. ****************************************************************/
  23. char * Employee::getDepartment()
  24. {
  25. return department;
  26. }
  27.  
  28. /****************************************************************
  29.   FUNCTION: Employee::getSalary()
  30.   ARGUMENTS: none
  31.   RETURNS: Salary amount
  32.   NOTES: Returns the salary for an individual
  33. ****************************************************************/
  34. double Employee::getSalary()
  35. {
  36. return salary;
  37. }
  38.  
  39. /****************************************************************
  40.   FUNCTION: Employee::print()
  41.   ARGUMENTS: none
  42.   RETURNS: none
  43.   NOTES: Prints the values of the data members
  44. ****************************************************************/
  45. void Employee::print()
  46. {
  47. cout << "Name ---- Department ---- Salary";
  48. }
  49.  
  50. int main()
  51. {
  52. return 0;
  53. }

So why cant i declare my Employee employee_list[100] in the main.cpp file? Am i not referencing the Class Employee correctly? Sorry if my code is awful, i havent coded anything in 3 months and forgot half of it!

Please any answers or questions to help me would be awesome. Just point me in the right direction, throw me a link so i can learn, anything would be great.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem declaring class object array

 
0
  #2
Jan 26th, 2008
>>I get a linker error with the above code
What is the error message ?

>>So why cant i declare my Employee employee_list[100] in the main.cpp file?
I thought you said you are getting a link error? link errors will not produce this error, but a compile error will. What is the exact compile error?
Last edited by Ancient Dragon; Jan 26th, 2008 at 8:25 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Problem declaring class object array

 
0
  #3
Jan 26th, 2008
[Linker error] undefined reference to `Employee::Employee()'
ld returned 1 exit status

Is the exact error i get. I am using dev c++.

The error is a 'linker error' so im assuming the files arent compiling correctly or seeing each other or something? What does this error really mean?

Thanks for the reply!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem declaring class object array

 
0
  #4
Jan 26th, 2008
Everything compiled ok, that error message just means it could not find the class constructor. Make sure method.cpp is part of the dev-cpp project.

One error is that the project has two main() functions. you need to delete one of them.
Last edited by Ancient Dragon; Jan 26th, 2008 at 9:32 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Problem declaring class object array

 
0
  #5
Jan 26th, 2008
You were right. The reason it wouldnt see the method.cpp file is that i was trying to compile the program within dev c++ but it wasnt set up as a project. Im unsure how to do this. Is it really as simple as starting a project and loading the files up into dev c++?

The way i have to compile the program is through UNIX. I created my own makefile and uploaded the 3 files, method.cpp main.cpp and employee.h onto the UNIX system, then called make , then it compiled just fine.

*** i was able to get Dev C++ to compile my program / project just fine. Thanks again for all the great help Dragon. If anything else comes up - ill post here thanks again
Last edited by tones1986; Jan 27th, 2008 at 12:01 am. Reason: Sorted out my Dev c++ by myself :) learning folks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Problem declaring class object array

 
0
  #6
Jan 27th, 2008
Another quick question. Under my class (within header file employee.h) i have firstName, lastName, department, and salary. If i am working from my main.cpp how can i access the private section of this class. I understand the only way to do this would be to be within one of the Classes methods correct? Within the main.cpp i am trying this:

  1. int buildEmployeeList(Employee employee_list[])
  2. {
  3. int count = 0;
  4.  
  5. ifstream inFile;
  6. inFile.open("employees1", ios::binary);
  7. if (inFile.fail())
  8. {
  9. cout << "Unable to open employees1 file\n";
  10. exit(1);
  11. }
  12. inFile >> employee_list[count].firstName;
  13. while (!inFile.eof())
  14. {
  15. inFile >> employee_list[count].lastName;
  16. inFile >> employee_list[count].department;
  17. count++;
  18. inFile >> employee_list[count].firstName;
  19. }
  20. inFile.close();
  21. return count;
  22.  
  23. }
I obviously get errors telling me these members are private! Duh!

(both my employee.cpp and employee.h files i posted above are unchanged from before)

I thought if i am working from an array type from the class created - it should have access aswell? no? Confused with permissions from outside of class then! Any way around this. Will keep working - has to be a logical way !Thanks in advance for suggestions or links for help with classes! (basic stuff please!)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Problem declaring class object array

 
0
  #7
Jan 27th, 2008
The whole idea of private data members is to prevent code outside the class from directly accessing them. Two reasons:
1. To hide the actual method of storing. Does it matter to main.cpp whether you use a char array based string or a string class item to hold the names? Should it matter to main.cpp if you later change how they are stored? No, not if you've constructed the class methods correctly.

2. To provide a means of validating the inputs before actually storing to the data member. Consider a date class. Would it make sense to allow a user of the class to directly set the day of month to 35?

So, you generally create public class methods that will allow the user program to pass in inputs, and the class method will then store that value to the private data member, if appropriate.

To solve your current programming problem, you probably should create get( ) and set( ) methods for each data element. Then in main read into temporary variables, and pass those values to the corresponding set( ) method.
Val
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Re: Problem declaring class object array

 
0
  #8
Jan 27th, 2008
THanks for the help once again Val. I got a reply from my TA in my c++ class about my question also. With both your help i figured out a way to solve my problem. At least i thought so --- here is what i did.

int buildEmployeeList(Employee employee_list[])
{
  int count = 0;
  //char first[16] = "\0", last[16] = "\0", depart[11] ="\0";
  ifstream inFile;
  inFile.open("employees1", ios::binary);
  if (inFile.fail())
    {
    cout << "Unable to open employees1 file\n";
    exit(1);
    }
   inFile.read((char *)&employee_list[count], sizeof(Employee));
   while (!inFile.eof())
     {
         count++;
     inFile.read((char *)&employee_list[count], sizeof(Employee));
     
     }
inFile.close();
for (int i = 0; i < 15; i++)

cout << "Department: " << employee_list[i].getDepartment()<< "Salary: " << employee_list[i].getSalary() << endl;
return count;

}
This is it working - it will print out my Department, and the salary, and looking at a example print out - it all lines up with correct department and figures. BUT ... how would i access the names still? I cant do:
[code]
employee_list[i].Employee::Employee(); (the constructor contains the first name, last name, and salary
  1. Employee::Employee()
  2. {
  3. firstName[0] = '\0'; //Sets null char to firstName
  4. lastName[0] = '\0'; //Sets null char to lastName
  5. salary = 0; //Sets salary to 0
  6. }

This is on my methods.cpp file. Im getting the hang of it (sort of, but would like confirmation that im on the right path - the answers just there ... i just can see it in the code...!)

Thanks once again in advance -
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Problem declaring class object array

 
0
  #9
Jan 27th, 2008
You already have getDepartment and getSalary accessor functions. Write accessor functions getLastName and getFirstName. They'll be almost identical to getDepartment.

  1. class Employee
  2. {
  3. private:
  4. char firstName[11];
  5. char lastName[16];
  6. char department[16];
  7. double salary;
  8. public:
  9. Employee();
  10. char *getDepartment();
  11.  
  12. char* getLastName (); // add this function.
  13. char* getFirstName (); // add this function.
  14.  
  15.  
  16. double getSalary();
  17. void print();
  18.  
  19. };
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,673
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Problem declaring class object array

 
0
  #10
Jan 27th, 2008
If in fact that infile.read( ) statement is working, it depends on your data file having exactly the right number of characters for each field (fname, lname, dept, oops, how's it going to read in the salary of type double?)

And you are still violating the premise that the data members are private, only to be directly accessed by class methods.

As I mentioned before, you should have methods that get and set each (or combinations of ) data element.
  1. void Employee::setDepartment( char * dept )
  2. {
  3. strcpy( department, dept );
  4. }
  5.  
  6. void Employee::setFirstname( char * fname )
  7. {
  8. strcpy ( firstName, fname );
  9. }
Here is where you build in protection. What if the user provided a first name that was 15 characters long? Copying that in full would then overwrite part of the last name, or run outside the bounds of the object's storage. Bad in either case. A better implementation would check the length of input and only store the portion that fits.
  1. void Employee::setFirstname( char * fname )
  2. {
  3. if( strlen( fname ) < 10 )
  4. strcpy ( firstName, fname );
  5. else
  6. {
  7. strncpy( firstName, fname, 10 );
  8. firstName[10] = '\0';
  9. }
  10. }
Now in main.cpp, you have:
  1. char fname[100] = ""; //big enough for any unreasonable input
  2. char lname[100] = ""; //etc.
  3.  
  4. infile >> fname; //or use getline if each data item on separate line in file
  5. employeeList[n].setFirstname( fname );
  6. //do the same read and set action for the other data elements
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC