redefinition error please help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 2
Reputation: kwongchungying is an unknown quantity at this point 
Solved Threads: 0
kwongchungying kwongchungying is offline Offline
Newbie Poster

redefinition error please help

 
0
  #1
Oct 23rd, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: redefinition error please help

 
0
  #2
Oct 23rd, 2004
Header files are just textual replacement. When you say
  1. #include "Person.h"
That line is replaced with the contents of Person.h. In other words, everywhere you include Person.h, you get this:
  1. #include <string>
  2.  
  3. class Person
  4. {
  5. public:
  6. Person(char * name = 0,int age = 0);
  7. Person(Person const & p);
  8. Person& operator=(Person const & rhs);
  9. virtual ~Person();
  10. void Display() const;
  11.  
  12. private:
  13. char* name_ ;
  14. int age_;
  15. };
So let's do a little test:
  1. class A {
  2. };
  3.  
  4. class A {
  5. };
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:
  1. #ifndef PERSON_H
  2. #define PERSON_H
  3.  
  4. class Person
  5. {
  6. public:
  7. Person(char * name = 0,int age = 0);
  8. Person(Person const & p);
  9. Person& operator=(Person const & rhs);
  10. virtual ~Person();
  11. void Display() const;
  12.  
  13. private:
  14. char* name_ ;
  15. int age_;
  16. };
  17.  
  18. #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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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