views:

228

answers:

3

I am working on a small game using C++, and I used Eclipse CDT's class generator. It created a .h file with the class definitions and a .cpp file that included body-less methods for said class.

So if I followed the template, I'd have a .cpp file filled with the methods declarations, and a .cpp file with method bodies. However, I can't include a .cpp file within another.

So what is the convention in C++ with classes and include files? What I did was fill in the method bodies right under the class declaration in the .h file, and deleted the .cpp file.

+4  A: 

You don't have to include the .cpp file. Including the .h file is all it takes. .h means header, ie, all it should have is function / object definitions. The actual implementations go in the .cpp file of the same name. The linker will deal with straightening it out for you.

Donnie
A: 

I'm not quite sure I understand. The header files defines what the class is and can do, and you include that into any source files that need to use the class.

The source file implements how the class does its action.

However, you can include a .cpp into another (you can include anything into anything), but you don't need to.

GMan
+1  A: 

One more thing you can do is while creating a header file is to use the preprocessor directive ifdef and endif. This will prevent your header file being included multiple times. This is a standard practice which I use whenever I create a new header file.

bakore