Question on Article "Headers and Includes: Why and How" (by Disch) -- inline!

Hi!

Regarding the article "Headers and Includes: Why and How":

Please see ** 7) Function inlining **
http://cplusplus.com/forum/articles/10627/#msg49682

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...

Thanks.

6 files below: X.h, X.cc, Y.h, Y.cc, main.cc, Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===== X.h =============================
#ifndef X_h
#define X_h


class Y;

class X
{
 public:
  X();
  void dereference_y_pointer(); // just declaration
  int i;
  Y *y;
};

#include "Y.h"
inline void X::dereference_y_pointer() // definition here
{
  i = y->i;
}

#endif



//===== X.cc =============================
#include "X.h"

X::X() : i(0) {}



//===== Y.h =============================
#ifndef Y_h
#define Y_h

#include "X.h"

class Y
{
 public:
  Y();
  int i;
  X x;
};

#endif



//===== Y.cc =============================
#include "Y.h"

Y::Y() : i(0) {}



//===== main.cc =============================
#include "Y.h"
#include "X.h"

int main(void)
{
  X x;
  Y y;
  return 0;
}



//===== Makefile =============================
main: main.o Y.o X.o
        g++ $^ -o $@

main.o: main.cc
        g++ -c $<

Y.o: Y.cc Y.h
        g++ -c $<

X.o: X.cc X.h
	g++ -c $<

clean:
      	rm *.o
Last edited on
only way I get it to work

What is the error you get when you have it inline?
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

Last edited on
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*


Good catch, and thanks for posting it.
Wow, yes it works if one pulls the include before the guard. I was trying around a bit, but didn't think of that! ;)

Thanks.
Topic archived. No new replies allowed.