0

Main.cpp

#include "Test1.h"
#include "Test2.h"

int main(){  
    Test1 t1;
    Test2 t2;

    t1.process(t2);
    t2.process(t1);

} 

Test1.h

#ifndef TEST1
#define TEST1

#include "Test2.h"

class Test1 {
public:
    void process(const Test2& t) {};
};


#endif // !TEST1

Test2.h

#ifndef TEST2
#define TEST2

#include "Test1.h"

class Test2 {
public:
    void process(const Test1& t) {};
};


#endif // !TEST2

VS2012 says:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '&'
error C2664: 'Test2::process' : cannot convert parameter 1 from 'Test1' to 'const int'

I am pretty sure it's the circular includes problem again (I come across it once in a while), but this time I am not sure why doesn't this compile.

Notice: the classes only depend on each other's references, which are of known size. Is it because of the include guards (#ifndef), that make one of the Test headers include the other as an empty file?

Kalamar Obliwy
  • 861
  • 2
  • 8
  • 13
  • 5
    Why do you need to include at all? Forward declaration is enough in both `*.h`. – kennytm May 06 '13 at 18:10
  • 1
    There are millions of duplicates of this. Make some effort to research the problem before posting. – Kerrek SB May 06 '13 at 18:10
  • Look [here](http://stackoverflow.com/questions/553682/when-to-use-forward-declaration), very good explanation. You can use it as a rule of thumb, when deciding whether to forward declare or to include. – Alexander Shukaev May 06 '13 at 18:10

5 Answers5

3

If you insist on doing this, you need to forward declare your class in each .h file so that the compiler knows what it is.

#include "Test1.h"

class Test1;

class Test2 {
public:
    void process(const Test1& t) {};
};
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
1

Fully expand the preprocessor directives and you'll see the problem: Main.cpp includes Test1.h which includes Test2.h, so class Test2 will be compiled first, before Test1 is ever defined, leading to the missing type specifier error. You can probably fix this by forward-declaring Test1 and saying void process(const class Test1& t) rather than just void process(const Test1& t).

Richard Walters
  • 1,464
  • 8
  • 16
  • but why do I need a definition of Test1 in Test2, I only use a reference in a method signature; its of known size. I dont even call any methods – Kalamar Obliwy May 06 '13 at 18:18
  • For C++ that's not enough; it doesn't know what `Test1` is at that point, so it doesn't know if it's a type or something else. Sure, C++ could infer that `Test1` is a type, but that's not how the language was designed. You have to explicitly say `Test1` is a type before you can use it; either declare the type fully, or forward-declare it as a type. – Richard Walters May 06 '13 at 18:21
0

You will need to put a forward declaration of one of them in the header of the other. If you forward declare Test1 in Test2.h (before declaring Test2), you can remove #include "Test1.h" from Test2.h.

Marc Claesen
  • 16,778
  • 6
  • 27
  • 62
0

If you have a reference or pointer to a type within a header just try using a forward declaration.

// Within stop Test2.h
class Test1;
RandyGaul
  • 1,915
  • 14
  • 21
0

In test1.h and test2.h you can use a forward declaration of Test2 and Test1 respectively avoiding the includes. Just put class Test1; in place of #include "test1.h".

Then include test1.h in the implementation file only.

See this question

Community
  • 1
  • 1
Danny
  • 2,221
  • 2
  • 18
  • 21