Export (0) Print
Expand All
Expand Minimize

Compiler Error C3831

'member': 'class' cannot have a pinned data member or a member function returning a pinning pointer

pin_ptr or __pin was used incorrectly.

The following sample generates C3831:

// C3831a.cpp
// compile with: /clr
ref class Y
{
public:
   int i;
};

ref class X
{
   pin_ptr<int> mbr_Y;   // C3831
   int^ mbr_Y2;   // OK
};

int main() {
   Y y;
   pin_ptr<int> p = &y.i;
}

The following sample generates C3831:

// C3831b.cpp
// compile with: /clr:oldSyntax
#using <mscorlib.dll>

__gc class Y
{
};

__gc class X
{
   Y __pin * mbr_Y;   // C3831
   Y * mbr_Y2;   // OK

   Y __pin * mf_Y()  // C3831
   {
      Y __pin * pY = new Y();
      return pY;
   }

   Y * mf_Y2()   // OK
   {
      Y * pY = new Y();
      return pY;
   }
};

Community Additions

ADD
Show:
© 2014 Microsoft