编译器错误 C2276

“operator”: 对绑定成员函数表达式的非法操作

编译器发现用于创建指向成员的指针的语法存在问题。

备注

尝试通过使用实例变量而不是类类型来限定成员来创建指向成员的指针时,通常会导致错误 C2276。 如果尝试使用错误的语法调用成员函数,也可能会看到此错误。

示例

此示例显示了 C2276 可能出现的几种方式以及修复方法:

// C2276.cpp
class A {
public:
   int func(){return 0;}
} a;

int (*pf)() = &a.func;   // C2276
// pf isn't qualified by the class type, and it 
// tries to take a member address from an instance of A.
// Try the following line instead:
// int (A::*pf)() = &A::func;

class B : public A {
public:
   void mf() {
      auto x = &this -> func;   // C2276
      // try the following line instead
      // auto x = &B::func;
   }
};

int main() {
   A a3;
   auto pmf1 = &a3.func;   // C2276
   // try the following line instead
   // auto pmf1 = &A::func;
}