Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) HeaderFileExample1


Header file example 1 shows what does and what does not belong in a header file [1].









  1. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, figure 1-3, page 28



Technical facts


Operating system(s) or programming environment(s)


Project type:

C++ standard:


Libraries used:






Qt project file: ./CppHeaderFileExample1/CppHeaderFile.pro


include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
# }
# QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

SOURCES += main.cpp

HEADERS += radio.h








#include "radio.h"

int main()


/* Compiler output:

15:38:36: Running steps for project CppHeaderFile...
15:38:36: Configuration unchanged, skipping qmake step.
15:38:36: Starting: "/usr/bin/make" -w
make: Entering directory `/home/richel/Projects/Test/CppHeaderFile-build-desktop-Qt_4_8_1__System__Release'
g++ -c -pipe -Wall -Wextra -Weffc++ -O2 -Wall -W -DQT_WEBKIT -I/usr/share/qt4/mkspecs/linux-g++ -I../CppHeaderFile -I../CppHeaderFile -I. -o main.o ../CppHeaderFile/main.cpp
In file included from ../CppHeaderFile/main.cpp:1:0:
../CppHeaderFile/radio.h:6:12: warning: 'LENGTH' initialized and declared 'extern' [enabled by default]
../CppHeaderFile/radio.h:29:15: error: conflicting declaration 'double Radio::S_PI'
../CppHeaderFile/radio.h:14:25: error: 'Radio::S_PI' has a previous declaration as 'const double Radio::S_PI'
../CppHeaderFile/radio.h:29:15: error: declaration of 'const double Radio::S_PI' outside of class is not definition [-fpermissive]
../CppHeaderFile/radio.h:31:5: error: redefinition of 'int Radio::size() const'
../CppHeaderFile/radio.h:22:12: error: 'int Radio::size() const' previously defined here
../CppHeaderFile/radio.h:8:12: warning: 'y' defined but not used [-Wunused-variable]
../CppHeaderFile/radio.h:9:13: warning: 'void func()' defined but not used [-Wunused-function]
make: Leaving directory `/home/richel/Projects/Test/CppHeaderFile-build-desktop-Qt_4_8_1__System__Release'
make: *** [main.o] Error 1
15:38:36: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project CppHeaderFile (target: Desktop)
When executing step 'Make'









//Code copied from:
// * John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 1.1.3, Figure 1-3, page 28.


int z;                              // illegal: external data definition
extern int LENGTH = 10;             // illegal: external data definition
const int WIDTH = 5;                // avoid: constant data definition
static int y;                       // avoid: static data definition
static void func() {/*...*/}        // avoid: static function definition

class Radio
    static int s_count;             // fine: static member declaration
    static const double S_PI;       // fine: static const member declaration
    int d_size;                     // fine: member data definition
    // ...
    int size() const;               // fine: member function declaration
    // ...
};                                  // fine: class definition

inline int Radio::size() const
    return d_size;
}                                   // fine: inline function definition

int Radio::s_count;                 // illegal: static member definition

double Radio::S_PI = 3.14159265358; // illegal: static const member definition

int Radio::size() const { /*...*/ } // illegal: member function definition







Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml