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;
}

Header files are just textual replacement. When you say

#include "Person.h"

That line is replaced with the contents of Person.h. In other words, everywhere you include Person.h, you get this:

#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_;
};

So let's do a little test:

class A {
};

class A {
};

Compile this and see which error you get. Mine is a redefinition error for class A. This is exactly what is happening in your program, and the easiest way to fix it is to wrap the header file in a conditional compilation statement:

#ifndef PERSON_H
#define PERSON_H

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_;
};

#endif

You'll also notice that I removed the include of <string> because it's not needed. You don't declare anything that requires a name from that header, so don't clutter up your code by including it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.