编译器错误 C2676

二元“operator”:“type*”不定义此运算符或到预定义运算符可接收类型的转换

注解

要使用该运算符,必须针对指定类型将其重载,或者定义一个到某个类型(该运算符已针对此类型进行了定义)的转换。

示例

下面的示例生成 C2676。

// C2676.cpp
// C2676 expected
struct C {
   C();
} c;

struct D {
   D();
   D operator >>( C& ){return * new D;}
   D operator <<( C& ){return * new D;}
} d;

struct E {
   // operator int();
};

int main() {
   d >> c;
   d << c;
   E e1, e2;
   e1 == e2;   // uncomment operator int in class E, then
               // it is OK even though neither E::operator==(E) nor
               // operator==(E, E) defined. Uses the conversion to int
               // and then the builtin-operator==(int, int)
}

如果尝试对引用类型的 this 指针执行指针算术,也可能会发生 C2676。

this 指针是引用类型中的类型句柄。 有关详细信息,请参阅 this 指针的语义

下面的示例生成 C2676。

// C2676_a.cpp
// compile with: /clr
using namespace System;

ref struct A {
   property Double default[Double] {
      Double get(Double data) {
         return data*data;
      }
   }

   A() {
      Console::WriteLine("{0}", this + 3.3);   // C2676
      Console::WriteLine("{0}", this[3.3]);   // OK
   }
};

int main() {
   A ^ mya = gcnew A();
}