hello,
i posted a thread before but i attached files so i guess it's too much trouble, i wish somebody can help me coz i have spent more than 5 hrs just to compile a very simple program, i have tried visual c ++ 6.0, g++ and gcc all have the same error, the error is as follows, if you guys think it's too troublesome to look at the codes, can you tell me what are the common reasons for redefinition errors, is it because of the way i compile or is there special way to compile? coz i tried many ways to compile the codes i still get the same errors.
g++
redefinition of class `person`
i have posted my code here there are 5 files i hope people can have a look and tell me what i can do for it coz i really become desperate and i can't find anything wrong in the code, thanks
main.cpp
#include <iostream>
#include "Person.h"
#include "Employee.h"
//---------------------------------------- main
using namespace std;
int main()
{
Person him("Superman", 30);
him.Display();
cout << endl << endl;
Employee her("Lois Lane", 36, 45000);
her.EmployeeDisplay();
return 0;
}
person.h
#include <string>
class Person
{
public:
Person(char * name = 0,int age = 0);
Person(Person const & p);
Person& operator=(Person const & rhs);
virtual ~Person();
void Display() const;
private:
char* name_ ;
int age_;
};
person.cpp
#include<iostream.h>
#include "Person.h"
using namespace std;
Person::Person(char* name,int age)
: name_(name),age_(age)
{
cout << "Person constructor called" << endl;
}
void Person::Display() const
{
cout << "Name = " << name_ << endl;
cout << "Age = " << age_ << endl;
}
Person::Person(Person const & p)
: name_(p.name_),
age_(p.age_)
{
cout << "Person copy constructor called" << endl;
}
Person& Person::operator=(Person const & rhs)
{
cout << "Person assignment operator called" << endl;
if (this == &rhs)
return *this;
name_ = rhs.name_;
age_ = rhs.age_;
return *this;
}
employee.h
#include "Person.h"
class Employee: public Person
{
public:
Employee(char * name = 0, int age = 0, float salary = 0);
Employee(Employee const & e);
Employee& operator=(Employee const & rhs);
virtual ~Employee();
void EmployeeDisplay() const;
private:
float salary_;
};
employee.cpp
#include <iostream.h>
#include "Employee.h"
Employee::Employee(char* name,int age, float salary)
: Person (name, age),
salary_ (salary)
{
cout << "Employee constructor called" << endl;
}
void Employee::EmployeeDisplay() const
{
Display(); // Call Person Display
cout << "Salary = $" << salary_ << endl;
}
Employee::Employee(Employee const & e)
: Person(e),
salary_(e.salary_)
{
cout << "Employee copy constructor called" << endl;
}
Employee& Employee::operator=(Employee const & rhs)
{
cout << "Employee assignment operator called" << endl;
if (this == &rhs)
return *this;
Person::operator=(rhs); // Call Person // assignment operator
salary_ = rhs.salary_;
return *this;
}