質問編集履歴

4

動かしたコードを追記

2021/07/05 10:04

投稿

Kchan_01
Kchan_01

スコア110

test CHANGED
File without changes
test CHANGED
@@ -267,3 +267,95 @@
267
267
  ❯ g++ main.cpp sample.cpp
268
268
 
269
269
  ```
270
+
271
+
272
+
273
+
274
+
275
+ ## 追記 2021/07/05 19:00
276
+
277
+
278
+
279
+ templateを使わない縛りで書いているので、std::function<void(std::string)>を使用して実現したいです。
280
+
281
+
282
+
283
+ ``` cpp
284
+
285
+ void Phonebook::addItem(int i, std::function<void(std::string)> fn, std::string s)
286
+
287
+ {
288
+
289
+ std::string line;
290
+
291
+
292
+
293
+ std::cout << s;
294
+
295
+ std::getline(std::cin, line);
296
+
297
+ while (line.empty())
298
+
299
+ {
300
+
301
+ std::cout << s;
302
+
303
+ std::getline(std::cin, line);
304
+
305
+ }
306
+
307
+ fn(line);
308
+
309
+ }
310
+
311
+
312
+
313
+ void Phonebook::add(int i)
314
+
315
+ {
316
+
317
+ addItem(i, &getContact(i).setFirstName, "frist name : ");
318
+
319
+ }
320
+
321
+ ```
322
+
323
+
324
+
325
+ 以下のエラーが出ます。
326
+
327
+
328
+
329
+ ```terminal
330
+
331
+ ❯ g++ -std=c++11 main.cpp sample.cpp && ./a.out
332
+
333
+ sample.cpp:24:16: error: cannot create a non-constant pointer to member function
334
+
335
+ addItem(i, &getContact(i).setFirstName, "frist name : ");
336
+
337
+ ^~~~~~~~~~~~~~~~~~~~~~~~~~~
338
+
339
+ 1 error generated.
340
+
341
+ ```
342
+
343
+
344
+
345
+ &を取ってもエラーが出て実行できませんでした。
346
+
347
+
348
+
349
+ ```terminal
350
+
351
+ ❯ g++ -std=c++11 main.cpp sample.cpp && ./a.out
352
+
353
+ sample.cpp:24:30: error: reference to non-static member function must be called
354
+
355
+ addItem(i, getContact(i).setFirstName, "frist name : ");
356
+
357
+ ~~~~~~~~~~~~~~^~~~~~~~~~~~
358
+
359
+ 1 error generated.
360
+
361
+ ```

3

不要な記述を削除

2021/07/05 10:04

投稿

Kchan_01
Kchan_01

スコア110

test CHANGED
File without changes
test CHANGED
@@ -172,8 +172,6 @@
172
172
 
173
173
 
174
174
 
175
- // test
176
-
177
175
  std::cout << s;
178
176
 
179
177
  std::getline(std::cin, line);

2

不要な情報を削除

2021/07/05 09:16

投稿

Kchan_01
Kchan_01

スコア110

test CHANGED
File without changes
test CHANGED
@@ -216,7 +216,7 @@
216
216
 
217
217
  ```terminal
218
218
 
219
- ❯ g++ main.cpp sample.cpp && ./a.out
219
+ ❯ g++ main.cpp sample.cpp
220
220
 
221
221
  sample.cpp:20:7: error: called object type 'void (Contact::*)(std::string)' is not a function or function pointer
222
222
 

1

エラーケースを具体的に記述

2021/07/05 09:01

投稿

Kchan_01
Kchan_01

スコア110

test CHANGED
File without changes
test CHANGED
@@ -150,93 +150,87 @@
150
150
 
151
151
 
152
152
 
153
- ## 試したこと
153
+ ## 試したこと
154
+
155
+
156
+
157
+ 追記 2021/07/05 17:59
154
158
 
155
159
 
156
160
 
157
161
  以下の関数を定義し、呼び出してみましたが、うまく行きません。
158
162
 
159
- 別クラスで定義されている関数を関数ポインタとして使用することはできないのでしょうか。
160
-
161
163
 
162
164
 
163
165
  ```cpp
164
166
 
165
- void Phonebook::addItem(int i, void (*fn)(std::string), std::string s)
167
+ void addItem(int i, void (Contact::*fn)(std::string), std::string s)
166
-
168
+
167
- {
169
+ {
168
-
170
+
169
- std::string line;
171
+ std::string line;
172
+
173
+
174
+
170
-
175
+ // test
171
-
172
-
176
+
173
- std::cout << s;
177
+ std::cout << s;
174
-
178
+
175
- std::getline(std::cin, line);
179
+ std::getline(std::cin, line);
176
-
180
+
177
- while (line.empty())
181
+ while (line.empty())
178
-
182
+
179
- {
183
+ {
180
-
184
+
181
- std::cout << s;
185
+ std::cout << s;
182
-
186
+
183
- std::getline(std::cin, line);
187
+ std::getline(std::cin, line);
184
-
188
+
185
- }
189
+ }
186
-
190
+
187
- fn(line);
191
+ fn(line);
188
192
 
189
193
  }
190
194
 
195
+
196
+
197
+ void Phonebook::add(int i)
198
+
199
+ {
200
+
201
+ Contact tmp = getContact(i);
202
+
203
+ addItem(i, Contact::setFirstName, "frist name : ");
204
+
205
+
206
+
207
+ }
208
+
191
- ```
209
+ ```
192
-
193
-
194
-
210
+
211
+
212
+
195
- いろいろな記述方法を試しました、うくいきません
213
+ このエラー
196
214
 
197
215
 
198
216
 
199
217
  ```terminal
200
218
 
201
- sample.cpp:25:13: error: use of undeclared identifier 'setFirstName'
202
-
203
- addItem(i, setFirstName, "frist name : ");
219
+ g++ main.cpp sample.cpp && ./a.out
204
-
205
-
206
-
220
+
207
- sample.cpp:25:24: error: reference to non-static member function must be called
221
+ sample.cpp:20:7: error: called object type 'void (Contact::*)(std::string)' is not a function or function pointer
208
-
222
+
209
- addItem(i, contact[i].setFirstName, "frist name : ");
223
+ fn(line);
224
+
210
-
225
+ ~~^
211
-
212
-
226
+
213
- sample.cpp:25:22: error: call to non-static member function without an object argument
227
+ sample.cpp:26:25: error: call to non-static member function without an object argument
214
-
228
+
215
- addItem(i, Contact::setFirstName, "frist name : ");
229
+ addItem(i, Contact::setFirstName, "frist name : ");
230
+
216
-
231
+ ~~~~~~~~~^~~~~~~~~~~~
217
-
218
-
232
+
219
- sample.cpp:25:13: error: 'Contact' does not refer to a value
233
+ 2 errors generated.
220
-
221
- addItem(i, Contact[i].setFirstName, "frist name : ");
222
-
223
-
224
-
225
- sample.cpp:25:13: error: 'Contact' does not refer to a value
226
-
227
- addItem(i, Contact.setFirstName, "frist name : ");
228
-
229
-
230
-
231
- sample.cpp:25:22: error: call to non-static member function without an object argument
232
-
233
- addItem(i, Contact::setFirstName, "frist name : ");
234
-
235
-
236
-
237
- sample.cpp:25:13: error: 'Contact' does not refer to a value
238
-
239
- addItem(i, Contact.setFirstName, "frist name : ");
240
234
 
241
235
  ```
242
236