Hey,

I have seen some code examples were people include there own files, e.g

#include <myFile.h>

do i just have to save a file called myFile.cpp?

and if so can i declare a function in myFile and then use it in untitled1 without declaring it just including myFile. Thanks.

You would actually name the file myFile.h, not myFile.cpp. When you #include something you are basically taking the contents of the header (*.h) file and dumping them into your current .cpp file. If the file you need to include is in your current working directory (or if you're specifying an absoltue path), use "". If the file is in one of your compiler's include directories, use <>.

It is a bad idea to define functions in a header file. If you have this in myFile.h:

int MyFunc()
{
    return 5;
}

and you have two .cpp files in your project, and they both #include myFile.h, you'll get an error, because you're trying to re-define a function, a no-no. Instead, have prototypes in your header files. Like:
myfile.h

int MyFunc();

and have definitions in a .cpp file:
myfile.cpp

#include "myfile.h"

int MyFunc()
{
    return 5;
}

Then any amount of other files can #include myfile.h with no problem, as long as myfile.cpp is also in the project.

One last thing, it's good to put the code in your header files in #ifndef blocks. Like this:

#ifndef MY_FILE_H
#define MY_FILE_H

int MyFunc();

#endif

This way, if you end up #including the same file twice in one .cpp file (which can happen when you have lots of #includes), you won't have an issue, since the code is only executed once.

commented: Nice post, easy to understand :P +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.