Sign in to follow this  
Fraza

Custom Header Files

Recommended Posts

Fraza    122
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
Agony    3452
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
Fraza    122
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
Zahlman    1682
<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

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