includes and headers

Started by
11 comments, last by dave 18 years, 5 months ago
Ok, it appears I'm an idiot. For the life of me I can't figure out how to create files in my project and have them link together. For instance, adding a class to a seperate file and creating instances of it from the main funtion. I've been searching the net for about 2 hours now just trying to find some real information on header files and how they actually work. So far all I've gotten are sites that only explain how to include them, not how to make them or what they really do etc. I even downloaded the code for a couple of MUDS to see if that would help but the ones I dl'd seem to have a header and a .cpp file pair (ie. player.h and player.cpp) but I can't figure out what they are actually doing. I want to find out how they relate. Can anyone point me in the direction of a tuturial that actually explains these in detail? So to sum it up... i'm an idiot and need help finding a resource that goes into the file structure of C++ programming (headers, linking mulitiple .cpp files, etc) and not just the code . Any help would be apprciated!
Advertisement
Typically header files contain the definitions for accompanying cpp files. So when you call a method that resides in a different CPP file the compiler looks for the definition of the function that you called. If the header is not in place then the compiler doesn't know what you are on about.

So to create an instance of an object in another file do this:

#include "picture.h"int main( int argc, char* argv[] ){  picture myPicture;  return 0;}

To expand on what i have said above. A C or CPP program is interpreted linearly, from top to bottom, as it is compiled into an executable or library. So the compiler works its way down from the top effectively keeping tabs on all the types that have been declared etc. Before you started separating code out into other files you would have to either put the function being called above the calling function, or put a declaration of the function above the calling function and the actual function below. Like this:

void FunctionBeingCalled();void CallingFunction(){}void FunctionBeingCalled(){}


See if you didn't have the declaration above the calling function then there is no way for the compiler to know that the function you are calling actually exists. When you include headers for other files you are effectively copying and pasting the code in the headers (which are declarations) above the calling functions. So with an entire class declaration in a class.h or whatever, the class is actually a new type which the compiler keeps tabs on.

Hope that helps.

ace
MyClass.h
#ifndef _MYCLASS_H_#define _MYCLASS_H_class MyClass{public:	MyClass();	void Method();};#endif


MyClass.cpp
#include "MyClass.h"MyClass::MyClass(){	// ...}void MyClass::Method(){	// ...}


Main.cpp
#include "MyClass.h"int main(){	MyClass myClass;	myClass.Method();	return 0;}
Well that helps a bit. Thanks for the quick reply. But I'm really looking for more in-depth explantions / examples. Something that would explain things such as creating a class in one file (.cpp file) and creating an instance of that class from other files.

You're explanation about how they relate is a great help. But I really need some form of tutorial I think (i doubt anyone here wants to go into the kind of detail I need). I found http://www.functionx.com/cpp/Lesson07.htm which is something along what I was looking for but it still doesn't explain it all very well (or doesn't explain it in a way that makes sense to me).
I see you guys were posting as I was doing the same thing. With the expanding explanation and the new post from Wavarian I think I have what I needed to at least get me going. :)

Thanks a bunch guys... time to go perform some more trial and error :)
I've written some more since you probably started replying, might help more.

ace
Quote:Original post by ace_lovegrove
Typically header files contain the definitions for accompanying cpp file. So when you call a method that resides in a different CPP file the compiler looks for the definition of the function that you called. If the header is not in place then the compiler doesn't know what you are on about.

So to create an instance of an object in another file do this:

*** Source Snippet Removed ***


Where picture.h looks like this:
/* inclusion guards - prevent same header from being included twice */#ifndef PICTURE_H#define PICTURE_Hclass picture {  public:  picture (); // constructor  ~picture (); // destructor  // other public stuff  private:  // private stuff};/* notice how everything here is simply a declaration,something to tell the compiler that a definition of thesespecific symbols will come later so go ahead and let me use them */#endif

meanwhile picture.cpp looks like:
#include "picture.h"/* include your own declarations so compiler understandswhat the heck you're defining */// proceed to define the functions you declared for the classpicture::picture () {  /* special constructor operations */}picture::~picture () {  /* destructor stuff */}picture::random_op_1 (int argument) { return something;}
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Well there ya go then!

Take care bud.

ace
lol, that was really quick :)
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
one more quick question regarding these lines of code:

#ifndef _MYCLASS_H_
#define _MYCLASS_H_



Does it have to be in all caps and does it have to have the _'s? I had created a .h, .ccp, and main.cpp files earlier and was not using all caps or _'s but did not get any error's compiling.

** edit **
just remembered that one of the mud code files I was looking at was using two _'s in front such as __name_h_ ... any difference?

This topic is closed to new replies.

Advertisement