Compiler Error C3351
Visual Studio 2015
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Compiler Error C3351.
object' : delegate constructor: second argument must be address of a static member function or global function
The compiler expected the address of a function declared static.
The following sample generates C3351:
// C3351a.cpp
// compile with: /clr
delegate int D(int, int);
ref class C {
public:
int mf(int, int) {
return 1;
}
static int mf2(int, int) {
return 1;
}
};
int main() {
System::Delegate ^pD = gcnew D(nullptr, &C::mf); // C3351
System::Delegate ^pD2 = gcnew D(&C::mf2);
}
The following sample generates C3351:
// C3351b.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>
__delegate int D(int, int);
__gc class C {
public:
int mf(int, int) {
// declare the function as static
// static int mf(int, int) {
return 1;
}
};
int main() {
C *pC = new C;
System::Delegate *pD = new D(0, &C::mf); // C3351
}
Show: