0

I'm having 4 files in my project and when I try to compile it, I get this error:

error C2011: 'Details' : 'class' type redefinition

I think it's because I've used #include"AClass.cpp" three times in my files and it loads three times, but when I remove the two other #include"AClass.cpp", the compilation fails because AClass isn't found in that files.

2
  • 1
    See stackoverflow.com/questions/8020113/c-include-guards Commented Dec 8, 2014 at 22:48
  • 3
    Don't include .cpp files, those are supposed to be implementation files. You should only be including header files (usually .h or .hpp). Commented Dec 8, 2014 at 22:49

1 Answer 1

1

As mentioned in the comments normally you don't include cpp-files at all. But anyway with header files you can run into the same situation. This is why most of the c++ headers are wrapped in the follwoing macro:

#ifndef SOME_HEADER_H
#define SOME_HEADER_H

class foo;

#endif //SOME_HEADER_H

For sure you have to replace SOME_HEADER with a unique name.

Of course there are situations (I had one of this cases short time ago), where you intend to include a header multiple times.. certain generics and some other magical things. But in general it is a good advise not to do so.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.