Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefinition error with multiple derived classes in the main

I have an Object base class, and I have several derived classes called Item, Person, and Location.

Because each of these are derived from Object I need to include Object.h in each of their header files, and I include all of the derived classes in my main.

Because I am doing that I am getting a redefinition error.

What I want to know is what is the correct way to include these files to avoid this error?

Thanks!

EDIT:

object.h

using namespace std;

class Object{
    string name;
    string description;

    public:
        Object();
        Object(string name, string description);
        void set_name(string name);
        void set_description(string description);
        string get_name();
        string get_description();
        ~Object();
};

item.h

using namespace std;

#include "object.h"

class Item : public Object{
    public:
        Item();
        Item(string name, string description);

};

locale.h

using namespace std;

#include "object.h"

class Locale : public Object{
    public:
        Locale();
        Locale(string name, string description);
};

main.cpp

#include <iostream>
#include <string>

#include "locale.h"
#include "item.h"


using namespace std;

int main(){
    return 0;
}
like image 245
jasonaburton Avatar asked Feb 26 '12 21:02

jasonaburton


1 Answers

Strange, everybody I've met that hits this problem does not have a slightest idea what is going on and you have properly analysed the problem.

Read this: http://en.wikipedia.org/wiki/Include_guard

like image 84
Wojciech Cierpucha Avatar answered Nov 06 '22 22:11

Wojciech Cierpucha