编译器错误 C2931

“class”: type-class-id 重新定义为“identifier”的成员函数

不能使用泛型或模板类作为另一个类的成员函数。

此错误在 Visual Studio 2022 及更高版本中已过时。

如果大括号匹配不正确,则可能导致此错误。

下面的示例生成 C2931:

// C2931.cpp
// compile with: /c
template<class T>
struct TC { };
struct MyStruct {
   void TC<int>();   // C2931
};

struct TC2 { };
struct MyStruct2 {
   void TC2();
};

使用泛型时也可能发生 C2931:

// C2931b.cpp
// compile with: /clr /c
generic<class T> ref struct GC {};
struct MyStruct {
   void GC<int>();   // C2931
   void GC2();   // OK
};