编译以下代码时出现以下错误。我很困惑,无法弄清楚这里出了什么问题。成员函数指针解引用错了吗?
错误:
#g++ fp.cpp
fp.cpp: In member function âvoid Y::callfptr(void (X::*)(int))â:
fp.cpp:33: error: no match for âoperator->*â in âpos ->* opâ
fp.cpp
#include <iostream>
#include <vector>
using namespace std;
class B {
// some base class
};
class X : public B {
public:
int z;
void a(int a) {
cout << "The value of a is "<< a << endl;
}
void f(int b) {
cout << "The value of b is "<< b << endl;
}
};
class Y : public B {
public:
int b;
vector<X> vy;
void c(void) {
cout << "CLASS Y func c called" << endl;
}
void callfptr( void (X::*op)(int));
};
void Y::callfptr(void (X::*op) (int)) {
vector<X>::iterator pos;
for (pos = vy.begin(); pos != vy.end(); pos++) {
(pos->*op) (10);
}
}