次の方法で共有


コンパイラ エラー 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;
}