Hey, I'm just starting to learn C++ and I need some help with a header file. I'm using a Dummies Guide book and it says that I need to create a header file with the extension .h. SO I created a source file called header.h and put this at the top of the main:

#include "header.h"

But when I compiled it I got an error message stating that all the functions and variables that were set in header.h were unknown to the main. So is there a problem with the way that I created/linked the header file?

This book really doesn't explain how to create header files, it just tells you to do so. And all the other C++ books I could find were outdated, with a copyright of 1993 (even older than me). I would really appreciate some help. Thanks.

Post the header file and the .cpp file. There's no specific special thing you need to do to create a header file. What you said could be correct, but it's a bit vague.

Some reading examples here, here and here.

Post the header file and the .cpp file. There's no specific
special thing you need to do to create a header file. What you said could be correct, but it's a bit vague.

main.cpp

#include <cstdlib>
#include <iostream>
#include "header.h"
using namespace std;
int main(int argc, char *argv[])
{
    variable = 20;
    function();
    system("PAUSE");
    return 0;
}

header.h

extern int variable;
void function();

You have prototyped the void function(); in the header file, but nowhere you have defined function(). How does the compiler know what it does?

Actually that's not the problem. The function was specified in another source file called proto.cpp. Anyway, I was playing around with the source files and somehow figured out how to make it work. But now there's a problem. I keep getting an error message and my console needs to close. Can you please help me out with this?

main.cpp

#include <cstdlib>
#include <iostream>
#include "header.h"
using namespace std;
int main(int argc, char *argv[])
{
    variable = 20;
    function();
    system("PAUSE");
    return 0;
}

proto.cpp

#include <cstdlib>
#include <iostream>
#include "header.h"
using namespace std;
int variable;
void function()
{
    cout << "Words" << endl;
    cout << endl;
    cout << "variable = " << variable << endl;
}

header.h

using namespace std;
extern int variable;
void function();

When I run this I get the correct output:

Words

variable = 20


But then I get an error message that says that Project.exe (my file) has encountered a problem and needs to close. I sent the error message, but why is this happening and how can I prevent it?

Hi, i don't have a compiler in front of me.... but the program is small and easy so here is something that should work.

main.cpp

#include <cstdlib>

// #include <iostream> 
//don't include things that you don't use...

#include "header.h"

//using namespace std; 
//you don't use anything from std namespace

int main()
{
    variable = 20;
    function();
    return 0;
}

proto.cpp

//#include <cstdlib> //you don't need cstdlib here...

#include <iostream>
#include "header.h"

using namespace std;

int variable;

void function()
{
    cout << "Words" << endl;
    cout << endl;
    cout << "variable = " << variable << endl;
}

header.h

#ifndef HEADER_H
#define HEADER_H

//using namespace std;
// don't use using directives inside header files...
// also here you don't need it anywhere....

extern int variable;
void function();

#endif

When I run this I get the correct output:

Words

variable = 20

But then I get an error message that says that Project.exe (my file) has encountered a problem and needs to close. I sent the error message, but why is this happening and how can I prevent it?

you shouldn't get an error message.... the code seems correct...{the corrections i made, were minor...and the program shouldn't crash. Try using a debugger....

Also there is a convention when you have a cpp file like mycode.cpp the corresponding header file should be named: mycode.h..

Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears. What are #ifndef and #define, and what's #endif? It works, thanks. But what are these three lines of code?

Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears. What are #ifndef and #define, and what's #endif? It works, thanks. But what are these three lines of code?

Why should it (stay open)? Unless you ask for input, the program will do all the code you ask it to, outputting everything, and once it's done (either when it reachers return 0; or the end-bracket of main) it will close. #define 'new symbol' 'previously known symbol' will make 'new symbol' and 'previously known symbol' look the same to the compiler. #ifndef 'symbol' checks if 'symbol' is #defined. If so, it will execute all the code after it until it reached something that tells it to stop, like #endif

Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears.

Look here

What are #ifndef and #define, and what's #endif? It works, thanks. But what are these three lines of code?

Those are called preprocessor directives.

Look here

I once read an article that said to use cin.get(). What's wrong with that?

Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears.

Look here

Or, to keep it on site, look here. :icon_wink:

Cool. Thanks for the info links. But I just have one more question. It was mentioned that

#define 'new symbol' 'previously known symbol' will make 'new symbol' and 'previously known symbol' look the same to the compiler.

But in the code that I got from n.aggel, there was #define HEADER_H. That's only one symbol, right? Or is it?

As was mentioned in one of the links, you can #define something with no value. The only real use for this is to use the symbol in preprocessor if-statements. One example is the #ifndef code blocks that sparked this discussion.

file1.h

#ifndef FILE_1
#define FILE_1
//the symbol FILE_1 now has a status of being defined, though it has no value

//class and function declarations, etc.
#endif

file2.cpp

//FILE_1 not defined
#include "file1.h"//code executed
//FILE_1 defined
#include "file1.h"//code not executed twice

Thanks.

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.