質問編集履歴

1

コード追加

2019/04/06 22:56

投稿

torimingo
torimingo

スコア122

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,179 @@
1
1
  クラスの前方宣言は「class ClassName;」となると思いますが、メンバ関数を前方宣言したい場合は、どのように書けばいいのでしょうか。
2
2
 
3
3
  ファイルを分割すれば問題ないことではあるのですが、ひとつのファイルに収めたいため、質問させて頂きました。
4
+
5
+
6
+
7
+ 以下にコードを示します。
8
+
9
+ ```C++
10
+
11
+ #include<vector>
12
+
13
+ #include<iostream>
14
+
15
+ using namespace std;
16
+
17
+
18
+
19
+ class Body;
20
+
21
+ class Parent;
22
+
23
+ class Child;
24
+
25
+
26
+
27
+ class Parent
28
+
29
+ {
30
+
31
+ public:
32
+
33
+ Parent(Body* body) :
34
+
35
+ mBody(body)
36
+
37
+ {
38
+
39
+ mBody->addParent(this);
40
+
41
+ }
42
+
43
+
44
+
45
+ void update()
46
+
47
+ {
48
+
49
+ updateParent();
50
+
51
+ }
52
+
53
+
54
+
55
+ virtual void updateParent()
56
+
57
+ {
58
+
59
+ }
60
+
61
+
62
+
63
+ private:
64
+
65
+ Body* mBody;
66
+
67
+ };
68
+
69
+
70
+
71
+ class Child : public Parent
72
+
73
+ {
74
+
75
+ public:
76
+
77
+ Child(Body* body) :
78
+
79
+ Parent(body)
80
+
81
+ {
82
+
83
+ }
84
+
85
+
86
+
87
+ virtual void updateParent() override
88
+
89
+ {
90
+
91
+ cout << "child" << endl;
92
+
93
+ }
94
+
95
+ };
96
+
97
+
98
+
99
+ class Body
100
+
101
+ {
102
+
103
+ public:
104
+
105
+ void initialize()
106
+
107
+ {
108
+
109
+ mChild = new Child(this);
110
+
111
+ }
112
+
113
+
114
+
115
+ void addParent(Parent* parent)
116
+
117
+ {
118
+
119
+ mParents.emplace_back(parent);
120
+
121
+ }
122
+
123
+
124
+
125
+ void runloop()
126
+
127
+ {
128
+
129
+ for(Parent* p : mParents){
130
+
131
+ p->update();
132
+
133
+ }
134
+
135
+ }
136
+
137
+
138
+
139
+ private:
140
+
141
+ Child* mChild;
142
+
143
+ vector<Parent*> mParents;
144
+
145
+ };
146
+
147
+
148
+
149
+ int main()
150
+
151
+ {
152
+
153
+ Body body;
154
+
155
+ body.initialize(); // オブジェクトの生成
156
+
157
+ body.runloop(); // メインループ
158
+
159
+
160
+
161
+ return 0;
162
+
163
+ };
164
+
165
+ ```
166
+
167
+ 以下のエラーが出力されます。
168
+
169
+ ```
170
+
171
+ test.cpp:15:8: error: invalid use of incomplete type ‘class Body’
172
+
173
+ mBody->addParent(this);
174
+
175
+ ```
176
+
177
+
178
+
179
+ ParentクラスからaddParent()が見えないといっている模様です。