After much research, I can't come away with the answer to why I'm seeing the following error message and I would appreciate some insight.
The error states:
error: redefinition of 'class Employee'
Before getting this message, I was in the midst of debugging other problems in my code. The last thing I changed (that I think is causing this error) was:
#include "EmployeeMain.cpp"
to my executable program file (called Employee.cpp).
Before adding this line, I saw 2 error messages (in the linking stage) that stated:
"Employee::displayEmployeeData()", referenced from:
_main in EmployeeMain.o
I'm sorry to sound terribly ignorant but I thought that I was getting these messages because they were referring to a file that I didn't include in the executable code in my Employee.cpp. Logically, I thought that meant I should include that file. But, like I mentioned above, after I added that #include statement, that's when I get this redefinition error.
Any help would be appreciated.
I've tired to keep this post short. If I'm sounding vague and/or if anyone willing to help needs more information, I' will quickly to post the full code for your perusal. (It's really not that much, but I feel like the more concise I am in my post, the better and quicker my responses.)
It is generally a bad idea to include your .cpp files. You should instead have your .cpp files include whatever .h(pp) files you need to have them work. If you could post your your main function that would be helpful.
You're not the first person I've heard this from. I wonder why my professor is teaching it that way. We'll see... Thanks. Here's the main function (FYI: each class has 3 data members, first name, last name, and monthly salary)
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "Employee.h" // include definition of Employee class
int main ()
{
// create 2 Employee objects
Employee Smith("John", "Smith", 4000);
Employee Sloth("Joe", "Sloth", 2);
Smith.displayEmployeedata(); // display values of Smith's data members.
Sloth.displayEmployeedata(); // display values of Sloth's data members.
return 0; // indicate successful termination
} // end main
Ok, on lines 6/7 you create two classes called Smith and Sloth, and on lines 9/10 you are suddenly calling them Obama/Palin. Try using Smith/Sloth (or switch the names of the ones you have created).
Yes, and I updated it again with the correction... (lol... I was trying to be funny in my project but when posting publicly I wanted to avoid any controversy so I changed the names but forgot to change those first two at the front of lines 9/10. I'm such a jerk... ::embarassed::)
Unfortunately the problem still exists after this.
I was just about to post the whole code that I've gotten thus far.
#include <string> // program uses C++ stanadard string class
using std::string;
//Employee class definition
class Employee
{
public:
Employee( string, string, float ); //constructor that initializes an Employee first name,
// last name, and monthly salary
//Function to set data members.
void setFirstName( string ); // function to set employee first name
void setLastName( string ); // function to set employee last name
void setSalary( float ); // funtion to set employee salary
//Function to get data members.
string getFirstName(); // function to get employee first name
string getLastName(); // function to get employee last name
float getSalary(); // function to get employee salary
//Function to display employee information
void displayEmployeedata();
private:
string firstName; // employee first name
string lastName; // employee last name
float monthlySalary; // employee monthly salary
}; //end Employee class definition
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "Employee.h" // include definition of Employee class
void Employee::setSalary( float salary )
{
if ( salary < 0 )
{
monthlySalary = 0.0;
cout << "monthly salary provided must be greater than 0." << endl;
cout << "monthly salary set to 0." << endl;
}
else
monthlySalary = salary;
} //end function set Salary
int main ()
{
// create 2 Employee objects
Employee Smith("Smith", "John", 4000);
Employee Sloth("Sloth", "Joe", 2);
Smith.displayEmployeedata(); // display values of Smith's data members.
Sloth.displayEmployeedata(); // display values of Sloth's data members.
return 0; // indicate successful termination
} // end main
Usually, you would put the definitions of the Employee class in a related .cpp, like Employee.cpp. Anyway, it seems that the function displayEmployeedata() and the getter/setter functions are not defined...that might be why it erroring.