A header file only contains declarations. A header file is meant to be included with the #include directive from potentially multiple source files before being compiled with the appropriate source files that contain the matching definitions of that header file's declarations.
Here is an example of a header file named "example.h":
#ifndef EXAMPLE_H
#define EXAMPLE_H
namespace example {
void f();
typedef long double number_t;
class c {
public:
int a, b;
bool c;
void d();
int e() const;
};
}
#endif
This header file contains a function declaration, typedef declaration, and a class declaration inside a namespace. The preprocessor directives in this header file prevent it from being included more than once within a single translation unit.
The source file, "example.cpp", that provides the definitions for this header file could look like:
#include "example.h"
#include <iostream>
void example::f()
{
std::cout << "f() called" << std::endl;
}
void example::c::d()
{
a = 0;
b = 0;
}
int example::c::e() const
{
return a + b;
}
The translation unit that contains your main function, let's call it "main.cpp", can then include "example.h", and use code declared from it:
#include "example.h"
#include <iostream>
int main()
{
example::c c;
c.d();
c.a = 5;
c.b = 7;
example::f();
std::cout << c.e() << std::endl;
return 0;
}
The compilation command for GCC would be: g++ main.cpp example.cpp