• Advertisement
Sign in to follow this  

question about header files

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have trouble making header files in Dev C++ 4.9.8.0 i open a blank source file, type in a class def, open another, type in it's function defs, and try to use it. guess what. i need someone to tell exactly what to do, step by step, to accomplish this thanks

Share this post


Link to post
Share on other sites
Advertisement
So you are writing the class declaration in the header file, foo.h, like:

class foo
{
private:
//stuff
public:
foo();
~foo();

void blah();
}


then you have the class member function difinitions in a seperate cpp file, foo.cpp, like so:

#include "foo.h"

foo::foo()
{
//init stuff
}

foo::~foo()
{
//clean up stuff
}

void foo::blah()
{
//function stuff
}


Note- you have the inclusion of foo.h that is in the projects local directory (hence "foo.h" as opposed to <foo.h>)

then you have a seperate file, say main.cpp, that has:


#include "foo.h"
foo *instfoo = NULL;
int main()
{
instfoo = new foo();

//stuff
}


Note- you include foo.h pointing to local directory and you create an instance of foo.

You should have this kind of setup to get modular C++ to work correctly.

Share this post


Link to post
Share on other sites
They don't have to be.. could do

#include "MyFolder\Spiffy.h"

Note also that if you use global variables do it like so


/* Foo.h */
extern bool Bar;

// ...

/* Foo.cpp */
bool Bar = true;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement