The Linux Page

Includes in your C++ files... and namespace "side effects"

Today I discovered that I couldn't include the QDebug header file at the time I need it.

When I write in C/C++ I like to add my test libraries at the point where I'm writing the debug function I'm working on so that way I can delete it all at once.

So the skeleton of a file would look something like this:

Comment (copyright/purpose of file)

#include of all headers necessary

code

#include of debug headers

debug code

However, today I had problems compiling and/or linking doing so. The qDebug() << ... expressions did not want to work.

Moving the #include of debug headers to the top (before any code) solved the problem.

This may be due to the fact that my #include is still inside a namespace and you obviously get the qDebug definition inside the namespace which is not allowed...

So the true skeleton was more like this:

Comment (copyright/purpose of file)

#include of all headers necessary

namespace my_stuff {

code

#include of debug headers

debug code

} // end my_stuff namespace

It still was a little surprised at the moment!