headers

Howdy,

I'm currently in a C++ class, and we're supposed to be using headers in our first project. Problem is, I know nothing about headers. I've googled it (google knows all C++) and I got a bunch of sites with weird inclusive commands that I've never learned. Any help at all is appreciated.

I mainly don't know what we're supposed to include in a header. I know you're supposed to prototype your functions, but... well, for some reason, my .cc's won't compile now.

Thanks
"I mainly don't know what we're supposed to include in a header"

You don't include stuff in header files, you include THE header file


just by writing

#include <HeaderFileName>

at the very top of the file.
like those headers.

1
2
3
4
5
#include <stdafx.h>
#include <iostream>
#include <time.h>
#include <fstream>
#include <string> 


no, I knew that. I was just wondering what we're supposed to write in CUSTOM header files. The ones included with quotation marks.

#include "header.h"

Like that. I don't know how to CREATE a header file.
Last edited on
A header file is a plain text file containing constants, variables, function prototypes, etc - so you can simply write it in any plain text editor and save it as "filename.h".

If you want to include code, then put this in a seperate .cpp file ("filename.cpp").

Here's a very simple example;

First a program which uses a function and a constant defined in a header file;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// header_howto.cpp
// a program which uses the howto.h header
//
#include <cstdlib>
#include <iostream>
#include "howto.h"

using namespace std;

int main(int argc, char *argv[])
{
    cout << "\n\n";
    // call a function - prototype in howto.h, code in howto.cpp
    hello();
    cout << "\n\n";
    // declare a variable and initialize to CRIKEY (found in howto.h)
    int yada_yada = CRIKEY;
    cout << yada_yada;
    cout << "\n\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Now the header file (howto.h)

1
2
3
4
5
6
7
8
9
// howto.h - header file

// constants
#define CRIKEY 100;

// variables

// function prototypes - BUT code is in howto.cpp
void hello();


and the code for the function hello() is in howto.cpp;

1
2
3
4
5
6
7
8
9
10
11
12
// howto.cpp
// code for header file howto.h
#include <cstdlib>
#include <iostream>
#include "howto.h"

using namespace std;

void hello()
{
     cout << "Hi World!";
}


I can only speak for how these are put together using Dev-Cpp, which is as follows;

1. Open a new project "header_howto"
2. Add the above three files "header_howto.cpp", "howto.cpp" and "howto.h" to the project.
3. Compile just like any ordinary single file program.

I believe that for a command line compiler such as cc you need only use;

 
cc header_howto.cpp howto.cpp


In broad outline, it's that easy.

Hope it helps,
Muzhogg
Last edited on
well, that was somewhat helpful. however, and I don't know if there's a difference, but I work with .cc files, not .cpp.

Anyone know anything?

Thanks
No real difference - they're both valid C++ source code filename extensions.

Which one you use depends on your compiler - Obviously yours uses .cc (although if you try you might find .cpp works anyway)

So everywhere you see ".cpp" in the above, just change to ".cc", and it should work just fine.

Easy peasy,
muzhogg
Last edited on
Topic archived. No new replies allowed.