Sign in to follow this  
furin121

includes and headers

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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_H

class 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 these
specific 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 understands
what the heck you're defining */


// proceed to define the functions you declared for the class

picture::picture () {
/* special constructor operations */
}

picture::~picture () {
/* destructor stuff */
}

picture::random_op_1 (int argument) {
return something;
}


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
No it doesn't, here you are simply saying to the compiler that you want to define a certain symbol if it hasn't already been defined. It can be any case you want although it is standard practice to do it in upper case. If it has been defined then the contents of the #ifndef #endif is left out. Underscores '_' are typically used in naming conventions in place of spaces. You may also notice in the future that there are quite alot of microsoft symbols with 2 leading underscores, so i personally don't use 2, only 1, and infrequently.

ace

Share this post


Link to post
Share on other sites
Cool beans. Well, just wanted to say thanks again guys. You were all a tremendous help. I've now got me some nice little include files working as intended and actually understand what's going on. (yes, i know this is probably pre-beginner level stuff but i was about to pull my hair out over this :) )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this