Jump to content
  • Advertisement
Sign in to follow this  
Fraza

Custom Header Files

This topic is 4350 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

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>

#include "Header.h"

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

#define __Header_H__

#include "Source.cpp"

void HelloWorld();

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

#include "Header.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 this post


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


Link to post
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 this post


Link to post
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.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!