質問編集履歴

2

Markdown形式に修正

2019/10/27 05:13

投稿

otyaken
otyaken

スコア9

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- # やりたいこと
1
+ # 前提
2
2
 
3
3
  データを保存しておくクラスSettingを作成し,データを渡す相手クラスによって処理を変更するような処理を実現したいです.
4
4
 
@@ -100,11 +100,243 @@
100
100
 
101
101
  ```
102
102
 
103
+ # 実装したいこと
104
+
105
+ このSettingクラスを別の関数に渡し.Setting::print関数を呼び出すようにしたいのですが,コンパイルエラーが発生してしまいました.
106
+
107
+ ```c++
108
+
109
+ /**
110
+
111
+ * これはNG
112
+
113
+ */
114
+
115
+ template<class Type, template<class>class Derived>
116
+
117
+ void NGprint(Setting<Type> &setting){
118
+
119
+ setting.print<Derived<Type>>();
120
+
121
+ }
122
+
123
+ int main(){
124
+
125
+ /**
126
+
127
+ * Settingクラスの初期化
128
+
129
+ */
130
+
131
+ Setting<double> setting;
132
+
133
+ setting.data = 1.0;
134
+
135
+
136
+
137
+ NGprint<double, Derived>(setting);
138
+
139
+ }
140
+
141
+ ```
142
+
143
+
144
+
145
+ # 試したこと
146
+
103
147
  これらのクラスを使って,Settingクラスが定義されている関数内からSetting::print関数を呼び出すことができました.
104
148
 
105
- ここで,このSettingクラスを別の関数に渡し.Setting::print関数を呼び出すようにしたいのですが,コンパイルエラーが発生してしまいました.
106
-
107
- ```c++
149
+ ```c++
150
+
151
+ /**
152
+
153
+ * main関数
154
+
155
+ */
156
+
157
+ int main(){
158
+
159
+ /**
160
+
161
+ * Settingクラスの初期化
162
+
163
+ */
164
+
165
+ Setting<double> setting;
166
+
167
+ setting.data = 1.0;
168
+
169
+
170
+
171
+ setting.print<Derived>();
172
+
173
+ }
174
+
175
+ ```
176
+
177
+
178
+
179
+ また,関数を渡すときにTypeをあらかじめ指定すればコンパイルできました.
180
+
181
+ ```c++
182
+
183
+ template<template<class>class Derived>
184
+
185
+ void OKprint(Setting<double> &setting){
186
+
187
+ setting.print<Derived>();
188
+
189
+ }
190
+
191
+
192
+
193
+ void main(){
194
+
195
+ /**
196
+
197
+ * Settingクラスの初期化
198
+
199
+ */
200
+
201
+ Setting<double> setting;
202
+
203
+ setting.data = 1.0;
204
+
205
+
206
+
207
+ OKprint<Derived>(setting);
208
+
209
+ }
210
+
211
+ ```
212
+
213
+
214
+
215
+
216
+
217
+ # 質問内容
218
+
219
+ どのように修正をすればよいでしょうか.
220
+
221
+ また,よりよい実装方法をご存じでしたら,ご教授願います.
222
+
223
+
224
+
225
+ # コード全文
226
+
227
+ ```c++
228
+
229
+ #include <cstdio>
230
+
231
+ #include <iostream>
232
+
233
+
234
+
235
+ /**
236
+
237
+ * クラスの前方宣言
238
+
239
+ */
240
+
241
+ template<class Type>
242
+
243
+ class Setting;
244
+
245
+
246
+
247
+ template<class Type, template<class>class Derived>
248
+
249
+ class Base;
250
+
251
+
252
+
253
+ template<class Type>
254
+
255
+ class Derived;
256
+
257
+
258
+
259
+
260
+
261
+ /**
262
+
263
+ * int型を保存しておくクラス
264
+
265
+ */
266
+
267
+ template<class Type>
268
+
269
+ class Setting{
270
+
271
+ public:
272
+
273
+ Type data;
274
+
275
+
276
+
277
+ public:
278
+
279
+ /**
280
+
281
+ * dataを使用するクラス
282
+
283
+ */
284
+
285
+ template<template<class>class Derived>
286
+
287
+ void print(){
288
+
289
+ Base<Type, Derived>::print(data);
290
+
291
+ }
292
+
293
+ };
294
+
295
+
296
+
297
+ /**
298
+
299
+ * 静的ポリモーフィズム基底クラス
300
+
301
+ */
302
+
303
+ template<class Type, template<class>class Derived>
304
+
305
+ class Base{
306
+
307
+ public:
308
+
309
+ static void print(Type data){
310
+
311
+ Derived<Type>::print(data);
312
+
313
+ }
314
+
315
+ };
316
+
317
+
318
+
319
+ /**
320
+
321
+ * 静的ポリモーフィズム派生クラス
322
+
323
+ */
324
+
325
+ template<class Type>
326
+
327
+ class Derived : Base<Type, Derived>{
328
+
329
+ public:
330
+
331
+ static void print(Type data){
332
+
333
+ std::cout << data << std::endl;
334
+
335
+ }
336
+
337
+ };
338
+
339
+
108
340
 
109
341
  /**
110
342
 
@@ -120,6 +352,12 @@
120
352
 
121
353
  }
122
354
 
355
+ /**
356
+
357
+ * main関数
358
+
359
+ */
360
+
123
361
  int main(){
124
362
 
125
363
  /**
@@ -139,165 +377,3 @@
139
377
  }
140
378
 
141
379
  ```
142
-
143
- どのように修正をすればよいでしょうか.
144
-
145
- また,よりよい実装方法をご存じでしたら,ご教授願います.
146
-
147
-
148
-
149
- # コード全文
150
-
151
- ```c++
152
-
153
- #include <cstdio>
154
-
155
- #include <iostream>
156
-
157
-
158
-
159
- /**
160
-
161
- * クラスの前方宣言
162
-
163
- */
164
-
165
- template<class Type>
166
-
167
- class Setting;
168
-
169
-
170
-
171
- template<class Type, template<class>class Derived>
172
-
173
- class Base;
174
-
175
-
176
-
177
- template<class Type>
178
-
179
- class Derived;
180
-
181
-
182
-
183
-
184
-
185
- /**
186
-
187
- * int型を保存しておくクラス
188
-
189
- */
190
-
191
- template<class Type>
192
-
193
- class Setting{
194
-
195
- public:
196
-
197
- Type data;
198
-
199
-
200
-
201
- public:
202
-
203
- /**
204
-
205
- * dataを使用するクラス
206
-
207
- */
208
-
209
- template<template<class>class Derived>
210
-
211
- void print(){
212
-
213
- Base<Type, Derived>::print(data);
214
-
215
- }
216
-
217
- };
218
-
219
-
220
-
221
- /**
222
-
223
- * 静的ポリモーフィズム基底クラス
224
-
225
- */
226
-
227
- template<class Type, template<class>class Derived>
228
-
229
- class Base{
230
-
231
- public:
232
-
233
- static void print(Type data){
234
-
235
- Derived<Type>::print(data);
236
-
237
- }
238
-
239
- };
240
-
241
-
242
-
243
- /**
244
-
245
- * 静的ポリモーフィズム派生クラス
246
-
247
- */
248
-
249
- template<class Type>
250
-
251
- class Derived : Base<Type, Derived>{
252
-
253
- public:
254
-
255
- static void print(Type data){
256
-
257
- std::cout << data << std::endl;
258
-
259
- }
260
-
261
- };
262
-
263
-
264
-
265
- /**
266
-
267
- * これはNG
268
-
269
- */
270
-
271
- template<class Type, template<class>class Derived>
272
-
273
- void NGprint(Setting<Type> &setting){
274
-
275
- setting.print<Derived<Type>>();
276
-
277
- }
278
-
279
- /**
280
-
281
- * main関数
282
-
283
- */
284
-
285
- int main(){
286
-
287
- /**
288
-
289
- * Settingクラスの初期化
290
-
291
- */
292
-
293
- Setting<double> setting;
294
-
295
- setting.data = 1.0;
296
-
297
-
298
-
299
- NGprint<double, Derived>(setting);
300
-
301
- }
302
-
303
- ```

1

改善

2019/10/27 05:13

投稿

otyaken
otyaken

スコア9

test CHANGED
File without changes
test CHANGED
@@ -84,6 +84,124 @@
84
84
 
85
85
  /**
86
86
 
87
+ * データをBaseクラスに渡して出力する関数
88
+
89
+ */
90
+
91
+ template<template<class>class Derived>
92
+
93
+ void print(){
94
+
95
+ Base<Type, Derived>::print(data);
96
+
97
+ }
98
+
99
+ };
100
+
101
+ ```
102
+
103
+ これらのクラスを使って,Settingクラスが定義されている関数内からSetting::print関数を呼び出すことができました.
104
+
105
+ ここで,このSettingクラスを別の関数に渡し.Setting::print関数を呼び出すようにしたいのですが,コンパイルエラーが発生してしまいました.
106
+
107
+ ```c++
108
+
109
+ /**
110
+
111
+ * これはNG
112
+
113
+ */
114
+
115
+ template<class Type, template<class>class Derived>
116
+
117
+ void NGprint(Setting<Type> &setting){
118
+
119
+ setting.print<Derived<Type>>();
120
+
121
+ }
122
+
123
+ int main(){
124
+
125
+ /**
126
+
127
+ * Settingクラスの初期化
128
+
129
+ */
130
+
131
+ Setting<double> setting;
132
+
133
+ setting.data = 1.0;
134
+
135
+
136
+
137
+ NGprint<double, Derived>(setting);
138
+
139
+ }
140
+
141
+ ```
142
+
143
+ どのように修正をすればよいでしょうか.
144
+
145
+ また,よりよい実装方法をご存じでしたら,ご教授願います.
146
+
147
+
148
+
149
+ # コード全文
150
+
151
+ ```c++
152
+
153
+ #include <cstdio>
154
+
155
+ #include <iostream>
156
+
157
+
158
+
159
+ /**
160
+
161
+ * クラスの前方宣言
162
+
163
+ */
164
+
165
+ template<class Type>
166
+
167
+ class Setting;
168
+
169
+
170
+
171
+ template<class Type, template<class>class Derived>
172
+
173
+ class Base;
174
+
175
+
176
+
177
+ template<class Type>
178
+
179
+ class Derived;
180
+
181
+
182
+
183
+
184
+
185
+ /**
186
+
187
+ * int型を保存しておくクラス
188
+
189
+ */
190
+
191
+ template<class Type>
192
+
193
+ class Setting{
194
+
195
+ public:
196
+
197
+ Type data;
198
+
199
+
200
+
201
+ public:
202
+
203
+ /**
204
+
87
205
  * dataを使用するクラス
88
206
 
89
207
  */
@@ -98,206 +216,88 @@
98
216
 
99
217
  };
100
218
 
219
+
220
+
221
+ /**
222
+
223
+ * 静的ポリモーフィズム基底クラス
224
+
225
+ */
226
+
227
+ template<class Type, template<class>class Derived>
228
+
229
+ class Base{
230
+
231
+ public:
232
+
233
+ static void print(Type data){
234
+
235
+ Derived<Type>::print(data);
236
+
237
+ }
238
+
239
+ };
240
+
241
+
242
+
243
+ /**
244
+
245
+ * 静的ポリモーフィズム派生クラス
246
+
247
+ */
248
+
249
+ template<class Type>
250
+
251
+ class Derived : Base<Type, Derived>{
252
+
253
+ public:
254
+
255
+ static void print(Type data){
256
+
257
+ std::cout << data << std::endl;
258
+
259
+ }
260
+
261
+ };
262
+
263
+
264
+
265
+ /**
266
+
267
+ * これはNG
268
+
269
+ */
270
+
271
+ template<class Type, template<class>class Derived>
272
+
273
+ void NGprint(Setting<Type> &setting){
274
+
275
+ setting.print<Derived<Type>>();
276
+
277
+ }
278
+
279
+ /**
280
+
281
+ * main関数
282
+
283
+ */
284
+
285
+ int main(){
286
+
287
+ /**
288
+
289
+ * Settingクラスの初期化
290
+
291
+ */
292
+
293
+ Setting<double> setting;
294
+
295
+ setting.data = 1.0;
296
+
297
+
298
+
299
+ NGprint<double, Derived>(setting);
300
+
301
+ }
302
+
101
303
  ```
102
-
103
- これらのクラスを使って,Settingクラスが定義されている関数内からSetting::print関数を呼び出すことができました.
104
-
105
- ここで,このSettingクラスを別の関数に渡し.Setting::print関数を呼び出すようにしたいのですが,コンパイルエラーが発生してしまいました.
106
-
107
- ```c++
108
-
109
- /**
110
-
111
- * これはNG
112
-
113
- */
114
-
115
- template<class Type, template<class>class Derived>
116
-
117
- void NGprint(Setting<Type> &setting){
118
-
119
- setting.print<Derived<Type>>();
120
-
121
- }
122
-
123
- int main(){
124
-
125
- /**
126
-
127
- * Settingクラスの初期化
128
-
129
- */
130
-
131
- Setting<double> setting;
132
-
133
- setting.data = 1.0;
134
-
135
-
136
-
137
- NGprint<double, Derived>(setting);
138
-
139
- }
140
-
141
- ```
142
-
143
- どのように修正をすればよいでしょうか.
144
-
145
- また,よりよい実装方法をご存じでしたら,ご教授願います.
146
-
147
-
148
-
149
- # コード全文
150
-
151
- ```c++
152
-
153
- #include <cstdio>
154
-
155
- #include <iostream>
156
-
157
-
158
-
159
- /**
160
-
161
- * クラスの前方宣言
162
-
163
- */
164
-
165
- template<class Type>
166
-
167
- class Setting;
168
-
169
-
170
-
171
- template<class Type, template<class>class Derived>
172
-
173
- class Base;
174
-
175
-
176
-
177
- template<class Type>
178
-
179
- class Derived;
180
-
181
-
182
-
183
-
184
-
185
- /**
186
-
187
- * int型を保存しておくクラス
188
-
189
- */
190
-
191
- template<class Type>
192
-
193
- class Setting{
194
-
195
- public:
196
-
197
- Type data;
198
-
199
-
200
-
201
- public:
202
-
203
- /**
204
-
205
- * dataを使用するクラス
206
-
207
- */
208
-
209
- template<template<class>class Derived>
210
-
211
- void print(){
212
-
213
- Base<Type, Derived>::print(data);
214
-
215
- }
216
-
217
- };
218
-
219
-
220
-
221
- /**
222
-
223
- * 静的ポリモーフィズム基底クラス
224
-
225
- */
226
-
227
- template<class Type, template<class>class Derived>
228
-
229
- class Base{
230
-
231
- public:
232
-
233
- static void print(Type data){
234
-
235
- Derived<Type>::print(data);
236
-
237
- }
238
-
239
- };
240
-
241
-
242
-
243
- /**
244
-
245
- * 静的ポリモーフィズム派生クラス
246
-
247
- */
248
-
249
- template<class Type>
250
-
251
- class Derived : Base<Type, Derived>{
252
-
253
- public:
254
-
255
- static void print(Type data){
256
-
257
- std::cout << data << std::endl;
258
-
259
- }
260
-
261
- };
262
-
263
-
264
-
265
- /**
266
-
267
- * これはNG
268
-
269
- */
270
-
271
- template<class Type, template<class>class Derived>
272
-
273
- void NGprint(Setting<Type> &setting){
274
-
275
- setting.print<Derived<Type>>();
276
-
277
- }
278
-
279
- /**
280
-
281
- * main関数
282
-
283
- */
284
-
285
- int main(){
286
-
287
- /**
288
-
289
- * Settingクラスの初期化
290
-
291
- */
292
-
293
- Setting<double> setting;
294
-
295
- setting.data = 1.0;
296
-
297
-
298
-
299
- NGprint<double, Derived>(setting);
300
-
301
- }
302
-
303
- ```