2

I ran into a weird situation. My understanding is that the order of include header files doesn't matter much if I have put #ifndef #define #endif flag to all .h files.

Old codes a.h

#ifndef A_H
#define A_H
   blah blah blah
#endif

a.cc

#include "a.h"
blah blah blah

And above codes worked fine.

Now I added a new header b.h

b.h

#ifndef B_H
#define B_H
   blah blah blah 
#endif

new a.cc

#include "a.h"
#include "b.h"
blah blah blah

The above a.cc compiled OK. However if I change the a.cc to

new a.cc version 2

#include "b.h"
#include "a.h"
blah blah blah

compiled failed with error: expected unqualified-id before ‘-’ token.

Sorry I cannot reproduce the same error in a tiny example. The compile error resulted in a big project. And if I tested in a small example created like above. It compiled, but if I switch back to the project. The #include directive order does matter. I have no idea where this problem might occur. Any one can give me a clue would be much helpful. Thanks in advance

[Solved] I've solved the problem by myself. Yet I think there might be other people stuck with it too. The reason which caused the problem was like the following

in test.cc

const int var_undef = -1;
#define var_undef (-1)

It compiles, while if you swap these two lines

#define var_undef (-1)
const int var_undef = -1

It compiles with the error expected unqualified-id before ‘-’ token as I stated.

Christopher
  • 253
  • 1
  • 4
  • 8
  • 2
    `#include` is essentially just a copy-and-paste process, so the include order might matter. Please work to produce a minimal test-case, otherwise there's no more help that we can give you. – Oliver Charlesworth Mar 19 '12 at 11:49
  • header file inclusion order doesnt matter unless you are inter mixing something – Rohit Vipin Mathews Mar 19 '12 at 11:50
  • The order *does* matter if the headers contain `#define` macros, or if there is some error at the end of `b.h`. A missing `;` or `}` perhaps? – Bo Persson Mar 19 '12 at 11:51
  • What no one else has mentioned yet is what is the reason for this error: "expected unqualified-id before ‘-’ token" ? I don't see any code here that would reproduce that so please produce a minimal example that exhibits your problem. –  Mar 19 '12 at 12:00
  • Include order *shouldn't* matter, but that is up to you to manage. That is, you need to fix *a.h* and *b.h* so that the order in which clients include your headers won't matter. It is not something that the compiler can do for you. BTW, you should start with the first compiler error, and add at least the two lines above the error to get a good answer, but my guess is that before that `-` there is something that is declared in *a.h* and not accessible in *b.h* at the point of the error. – David Rodríguez - dribeas Mar 19 '12 at 12:07

1 Answers1

3

The include order matters, of course. The include directive basically copy-pastes the content of the header in the current translation unit. If a type needed by b.h is defined in a.h, you need to include a.h before b.h, or better yet, include a.h in b.h.

Assume:

//a.h
struct A
{
};

//b.h
struct B : public A
{
};

//main.cc
#include "a.h"
#include "b.h"
int main()
{
   return 0;
}

This would compile fine as A is defined before B. The translation unit would basically be:

struct A
{
};
struct B : public A
{
};
int main() 
{
   return 0;
}

If, however, you inverse the order of includes, you'd get:

struct B : public A
{
};
struct A
{
};
int main() 
{
   return 0;
}

which is obviously an error.

However, the most correct way of doing it is including a.h in b.h:

//b.h
#include "a.h"
struct B : public A
{
};

That way, whoever wants to include b.h can without worrying about other headers.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625