I followed that recipe...
But I cannot get the code below to compile, if I use the inline function dereference_y_pointer() - see line 18 below.
The only way I get it to work, is if lines 17 to 21 are moved to line 29: i.e. if the header and function dereference_y_pointer() is moved to the source file X.cc (and thus NOT inline). See here: http://www.cplusplus.com/forum/general/46238/#msg251171
So my question: can we get the code below to compile?
Otherwise the Article might need an update...
Here's the error: superman@aaa:~/tmp$ make
g++ -c main.cc
In file included from Y.h:5:0,
from main.cc:5:
X.h: In member function ‘void X::dereference_y_pointer()’:
X.h:20:8: error: invalid use of incomplete type ‘struct Y’
X.h:6:7: error: forward declaration of ‘struct Y’
make: *** [main.o] Error 1
I see what's happening. That is definitely an error in the article. I'll have to update that.
The solution here is to move #include "X.h" outside Y's include guard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//===== Y.h =============================
#include "X.h" // <- put before the include guard
#ifndef Y_h
#define Y_h
class Y
{
public:
Y();
int i;
X x;
};
#endif
I'll have to think about it more before I can tell whether or not this will have other side-effects (I don't think it will, other than ever-so-slighly longer compile time).
I'll also have to think about a guideline that will be appropriate. Perhaps include dependencies should always be outside the guard? *shrug*