Compiler Error C3831
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 C3831.
member': 'class' cannot have a pinned data member or a member function returning a pinning pointer
pin_ptr (C++/CLI) 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;
}
};
Show: