Hi! I've been making a rather large (but not huge, by any means) program and it needs, or so I think, a couple of classes and I want them to use inheritance to make my life easier. However, when I try to include all of my files in the main file, I get errors saying that the class that all of the other classes are inheriting from is being redefined.

How can I make the class be defined once and yet #include it in multiple files that go on to be included in the same file.

I thought that maybe some sort of #undef preprocessor command might work...


Thanks, JT

Never use #include to include one *.cpp file in another. If you have two or more *.cpp files then they must be compiled separately and then linked together to create the final executable program.

Your class declaration should be in header files (with *.h extension). You can put them all in the same header file if you wish, or split them up into different header files. For small classes you might as well just put them all in the same header.

To avoid the duplicate declaration error problem you should use header code guards preprocessor directives, as in this example

#ifndef MYHEADER_H
#define MYHEADER_H
// put all your classes and other stuff here

#endif // end of MYHEADER_H

Thanks, I've had that problem in a bunch of different spots so this is greatly appreciated.

Thanks.
JT

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.