• 14
• 12
• 9
• 10
• 13

# Custom Header Files

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

## Recommended Posts

It's been a while since I used them in C++... So I tried to go all the way down to the most basic usage of a header file, but I can't get it to compile. Here's my code, can someone please put me out of my misery and tell me what I'm doing wrong? Main.cpp
#include <iostream.h>

int main()
{
cout << "Hello!" << endl;
HelloWorld;
system("PAUSE");
return 0;
}
#ifndef __Header_H__

#include "Source.cpp"

void HelloWorld();

#endif
Source.cpp
#include <iostream.h>

void HelloWorld()
{
cout << "Hello world!" << endl;
}
Main.cpp > Main application Header.h > Header file Source.cpp > Auxilary file for the header. The errors I was given by the compiler (Dev-Cpp 4.9.9.2) stated that the HelloWorld function was redefined, whereas I wanted it to be a function prototype in the header. I've considered that perhaps I need to do something special to add a header file to a project in Dev-Cpp? Because I copied and pasted code directly from a tutorial which then failed to compile. Thanks in advance for help.

##### Share on other sites
Remove the #include "Source.cpp" from your header file.

##### Share on other sites
... and make sure you link to the compiled source.o or whatever, so that you don't get undefined external symbols.

##### Share on other sites
Secondarily, add "()" to your HelloWorld function call in main(). Right now you just have "HelloWorld;", when you should instead have "HelloWorld()".

Also, you should #include <iostream>, not <iostream.h>. The latter was an old version, created before the C++ Standard was finished. It might behave slightly differently and/or not get updated appropriately.

This article (Organizing Code Files in C and C++) might be of some help, too.

##### Share on other sites
Thanks! There must have been a misprint in the tutorial I was using. Anyway, thanks for sorting that for me, I'm happy again.

Edit: if I put, "#include <iostream>" it doesn't work. The main project I'm doing uses DirectX, anyway. The only reason I forgot the brackets is because I'm still very used to Pascal.

##### Share on other sites
<iostream> puts things into the std namespace.

Whatever reference you learned iostream.h from needs to be destroyed. It is many years old and is now spreading misinformation.

Proper code looks like:

#include <iostream>// ...int main() {  std::cout << "outputting something" << std::endl;}

Or

#include <iostream>// These declarations can also be scoped, e.g. if you put them inside main()// at the top, they apply only to main()using std::cout;using std::endl;// ...int main() {  // Because you are 'using' std::cout, the compiler knows that cout here  // means std::cout.  cout << "outputting something" << endl;}

Or

#include <iostream>using namespace std; // Try to look up *every* symbol in std::// ...int main() {  cout << "outputting something" << endl;}

Also, don't use underscores at the beginning of your include-guard name. A double underscore, or a single underscore followed by capital letter, indicates a name reserved for use by the implementation.