Why not define functions in header files?

I'm just wrapping up a chapter on Memory Models and Namespaces, and am a bit confused about header files. I don't understand why you're not supposed to define functions in header files, but its fine to define function prototypes (other than the use of having different definitions in different functions)? Is it because when you put the definition in a header file and include that file in numerous other files, the function is defined many times?
I'm also wondering why defining inline functions in header files is okay? I assume that it has something to do with the way the computer manages memory with inline compared to normal. Also, is the keyword auto ever used? If anyone has any good reads on header files and the like, then feel free to share it, more info on these would be nice.
It has to do with the way programs are compiled. Assume for the following each header file ends in .h, and each source file ends in .cpp. When you compile a single source file, it will take every .h and .cpp and litteraly paste it on the line #include "AwesomeClass.cpp to produce a .o (object) file, that on its own, is quite useless. It would litterally be the same, as if you didn't have a second .cpp file, and just wrote all the code in one giant .cpp. All the prototypes stored in the headers provide means so that during the next step, when the linker.... links all the object files together, that each object will be able to properly call the functions in other objects.

The implication of this setup is, if you have 100 .cpp files, and you make 1 more that includes each, you would have to compile 101 files into a single object. Large projects would get even larger, and it is very redundant. If you have 100 .h files, you are only compiling comparitivly tiny symbols so that the linker can link to the precompiled .o file.
That's right; it would be defined multiple times, which is an error.

Inline functions are defined in header files because, in order to inline the function, the compiler needs to have the body of the function available when compiling the including source file.

auto is pretty much never used. It is getting a new meaning in the new standard, though.
Much better now. Header files always seemed mysterious. :? Thanks for the quick answers. One more question though, what is auto's new meaning in the new standard? Also, when is the new standard coming out?
New standard off the top of my head is supposed to be like late 2012, early 2013.

auto will automatically select the correct variable type, so you can have the following:

1
2
3
auto x = 3; //int
auto y = 2.3; //float
auto racing = "car" //array of char 
Not sure if it would do implicit conversions if you then later mixed and matched, such as making y= 'c' //y now char

Look up the wikipedia article on c++0x
The new standard is expected to come out this year (hence why it's called C++11 now).
You can't change a variable type later on, however auto is most useful when dealing with very long types or types unknown to you.

1
2
3
4
5
6
7
8
auto it=vec.begin(); //just saves typing work

template <class T,class U> void foo(T a,U b)
{
  auto diff=a-b; //auto select type, as which depends on the template parameters
  [...]

auto foo=[](int x) {return x*x;}; //can select type for a "strange" object, like a lambda function 
All you want to know about headers and more here...

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

...a must read for all beginner C++ programmers IMHO, AND IT'S FREE!!!

After that, the Scott Meyers Effective C++ books should be mandatory.
Topic archived. No new replies allowed.