“pos ->* op 中的操作符 ->* 不匹配”

use*_*533 3 c++ pointers function

编译以下代码时出现以下错误。我很困惑,无法弄清楚这里出了什么问题。成员函数指针解除引用是否错误?

\n\n

错误:

\n\n
#g++ fp.cpp\nfp.cpp: In member function \xc3\xa2void Y::callfptr(void (X::*)(int))\xc3\xa2:\nfp.cpp:33: error: no match for \xc3\xa2operator->*\xc3\xa2 in \xc3\xa2pos ->* op\xc3\xa2\n
Run Code Online (Sandbox Code Playgroud)\n\n

程序文件

\n\n
#include <iostream>\n#include <vector>\nusing namespace std;\n\nclass B {\n // some base class\n};\n\nclass X : public B {\n public:\n  int z;\n  void a(int a) {\n    cout << "The value of a is "<< a << endl;\n  }\n  void f(int b) {\n    cout << "The value of b is "<< b << endl;\n  }\n};\n\nclass Y : public B {\n public:\n  int b;\n  vector<X> vy;\n  void c(void) {\n   cout << "CLASS Y func c called" << endl;\n  }\n  void callfptr( void (X::*op)(int));\n};\n\nvoid Y::callfptr(void (X::*op) (int)) {\n vector<X>::iterator pos;\n for (pos = vy.begin(); pos != vy.end(); pos++) {\n  (pos->*op) (10);\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

And*_*owl 5

而不是这样做:

(pos->*op) (10);
Run Code Online (Sandbox Code Playgroud)

做这个:

((*pos).*op)(10);
Run Code Online (Sandbox Code Playgroud)

迭代器不需要提供 的重载operator ->*。如果你真的想使用operator ->*而不是operator .*,那么你可以这样做:

((pos.operator ->())->*op)(10)
Run Code Online (Sandbox Code Playgroud)

但这只是更冗长。

可能与您的用例相关的区别是运算符->*可以重载,而operator .*不能重载。