C++ include files

So I'm writing some classes for the game I'm working on and am packaging my classes into header files.

My question is this: what of a class definition, goes into the .h file and what goes into an associated .cpp file? at the moment it works for me to just dump the whole class (member functions, prototype and all) into the .h file, but that seems like it defeats the intended purpose of a header file.

The comments above are mostly right, but leave off a few corner cases. I'll be the pedantic a--hole, and fill in the details.

Code in the header file should include the declaration for the class, and any inline member functions. Although some compilers can optimize calls across object file boundaries, it's an optimization pass during/after the link phase. In general, I put all 2-5 line member functions in the .h file (think setters and accessors, and simple functions). All else goes in the .cpp file.

This leaves off one other corner case in C++, namely template classes. When dealing with templated classes, most compilers require you to put the definition in the .h file as well. The reason is because of when templates are instantiated during typical C++ compilation process.

The latest full standard of C++ includes support for the "export" (I think) keyword that eliminates the need to have template definitions in .h files, although not all compilers support it. I don't know the present state of g++ on the mac, so somone with more intimiate knowledge should chime in. (Additionally, using the export keyword causes an additional phase in the linker, so links could be slow.)

In general, here are my rules.

- Templated classes/functions -- all code goes in the header (mostly for portability between compilers, you know)
- All short member functions go in header
- All else in the .cpp file.

One other note. Per the latest C/C++ standards, I think you can put // comments on preprocessor lines. I think in old C compilers (think before C-9X) you couldn't use // comments on these lines because the ANSI preprocessor didn't recognize // comments at all. I don't have my copy of the standards here, but if the preprocessor won't accept // lines, and the standard allows it, I'd fill a bug report with the gcc team.

Ok, another question on a somewhat similar topic. I have non-OO code in C++ that I want to split up into multiple source files because it's too much for my brain in one long file.

I've put some processing functions (definitions) into a .cpp file, and function prototypes, structure definitions, and standard library includes into a .h file. I've included the .h file from the main, and included the .cpp (function definitions) from the .h file.

Xcode doesn't like this. It seems to make senese to me, but it looks like it's trying to compile the .cpp file by itself instead of including it in the others. (Yes, I'm somewhat new to c++). Any suggestions?

/**
This is a simple hierarchy. main.cpp->process.h->process.cpp
All with #inlcude
**/

put function prototypes, type declarations and constant definitions in .h files. Remember to use header guards or #pragma once. Put function definitions in .cpp files. Never #include a .cpp file. #include the .h file that contains prototypes, type declarations and constant definitions before you use them (whether that use is in a .h or a .cpp).

Ok, well I got it all formatted correctly now, but I'm consistantly getting the error " error: 'string' does not name a type". I tried including <string> but even that didn't help. I'm assuming you don't include the .cpp file becuase the compiler automatically seletcs it as a compile target if it's part of the project?