Jassaway
Jassaway

Reputation: 41

How to resolve a C++ Error: Redefinition of 'class'

I am new to C++ programming and have a compiler error that I can't figure out. Any help would be appreciated.

Here is the build log:

C:\Dev\MemberTest\Entity.cpp|6|error: redefinition of 'class Entity::Entity'|
C:\Dev\MemberTest\Entity.h|6|error: previous definition of 'class Entity::Entity'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

The program has Main.cpp, Entity.h and Entity.cpp (I was just tinkering with how to implement headers and source files).

#include <iostream>
#include "Entity.h"

int main()
{

    Entity::Entity Person("Grant", true); //Create person and set membership

    std::cout << Person.getName() << " is a member: " << Person.getMembership() << std::endl;

    return 0;
}
#ifndef ENTITY_H_INCLUDED
#define ENTITY_H_INCLUDED

namespace Entity
{
class Entity
{

    private:
        std::string name;
        bool member;

    public: //Get, set, constructor calls for a bool and string.
        Entity(std::string y, bool x);
        bool getMembership();
        std::string getName();
        void setMembership(bool x);

};
}

#endif // ENTITY_H_INCLUDED
#include <string>
#include "Entity.h"

namespace Entity
{
class Entity
{
    private:
        std::string name;
        bool membership;

    public:
        Entity(std::string y, bool x):name(y),membership(x){}
        bool getMembership(){return this->membership;};
        std::string getName(){return this->name;};
        void setMembership(bool x){this->membership=x;};

};
}

I've looked around for a solution and found questions like this: error: redefinition of class but the solutions I'm seeing aren't relevant to my program because I'm already using #ifndef.

Since I'm not sure what other info might be needed here goes: All three files are in the same folder and there aren't other source or header files in that folder. Oddly enough if I comment out the #include "Entity.h" in the Entity.cpp file and reference the source in Main.cpp instead of Entity.h it compiles and runs fine. I'm coding on Code::Blocks and with the GCC Compiler. Thanks again for any help.

Upvotes: 2

Views: 9476

Answers (1)

Acorn
Acorn

Reputation: 26066

The implementation file (Entity.cpp) should not contain the class definition again. Instead, you write non-inline definitions ("out of class"):

#include <string>
#include "Entity.h"

namespace Entity
{

Entity::Entity(std::string y, bool x) : name(y), membership(x) {}
bool Entity::getMembership() { return membership; }
std::string Entity::getName() { return name; }
void Entity::setMembership(bool x) { membership = x; }

}

Also note that your Entity.h header depends on std::string which requires the #include <string> header there, not just in the implementation file (Entity.cpp). There is no need to use this-> here nor some of the semicolons (;).


Oddly enough if I comment out the #include "Entity.h" in the Entity.cpp file and reference the source in Main.cpp instead of Entity.h it compiles and runs fine

That is because you can define functions inline in the class (instead of putting them in the implementation file). What you did is implement all of them in the class definition, and therefore you don't need an implementation file anymore.

In other words, your Entity.cpp looked like a header file with a full implementation of the class, although you called it .cpp rather than .h. Thus if you include that file, it would work.

Upvotes: 1

Related Questions