| | |
redefinition error please help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 2
Reputation:
Solved Threads: 0
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;
}
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
That line is replaced with the contents of Person.h. In other words, everywhere you include Person.h, you get this:
So let's do a little test:
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:
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.
C++ Syntax (Toggle Plain Text)
#include "Person.h"
C++ Syntax (Toggle Plain Text)
#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_; };
C++ Syntax (Toggle Plain Text)
class A { }; class A { };
C++ Syntax (Toggle Plain Text)
#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
I'm here to prove you wrong.
![]() |
Similar Threads
- Compile Error: Initializing an instance of a templatized class (C++)
- Error Message "Redifinition of `Struct ...' (C)
- HELP ERROR (C)
- Confused (C)
- redefintion errors (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Help Files
- Next Thread: writing a class without the class declaration?
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






