Export (0) Print
Expand All
Expand Minimize
0 out of 1 rated this helpful - Rate this topic

Compiler Error C2726

Error Message

'gcnew' may only be used to create an object with managed type

You cannot create an instance of a native type on the garbage-collected heap.

The following sample generates C2726:

// C2726.cpp
// compile with: /clr
using namespace System;
class U {};
ref class V {};
value class W {};

int main() {
   U* pU = gcnew U;    // C2726
   U* pU2 = new U;   // OK
   V^ p2 = gcnew V;   // OK
   W p3;   // OK

}

The following sample generates C2726:

// C2726b.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>
using namespace System;

class U {};
__gc class V {};

int main() {
   U* pU = __gc new U;    // C2726
   U* pU2 = new U;
   V* p2 = __gc new V;
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.