Multiple Files and Header Files

1
2
3
4
5
6
7
#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED

int add(int, int);

#endif // ADD_H_INCLUDED


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "add.h"  //imported the header file

using namespace std;

int add(int x, int y);

int main()
{
    cout << "Hello world!" << endl;
    cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
    return 0;
}

Two questions:
First, how are the definitions of the functions inside the header files associated with the declarations in the header files. (i.e how does my header know which add function I am talking about. The C++ source codes have to be in one folder or one project?
Secondly, what is a header guard word (in this case: "ADD_H_INCLUDED" ).
What is its purpose?

Finally, when I try to import a standard library to my add.cpp file:
1
2
3
4
5
6
7
#include <iostream>

int add(int x, int y)
{
    return(x+y);
}

I get: fatal error. No such directory exists.
I find that this is a big problem because if I can't import standard libraries into other files... well I can definately see the downside of that.

Please note that main.cpp was made when I created the empty project.
I added in an empty file later on and called it add

I thank you all so much for the help!

Edit: It turns out I was making a .c file. Nvrmind about this one and thanks.
Last edited on
Topic archived. No new replies allowed.