Compiler Error C3271

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

For the latest documentation on Visual Studio 2017, see Compiler Error C3271 on docs.microsoft.com.

'member': invalid value 'value' for the FieldOffset attribute

A negative number was passed to the FieldOffset attribute.

The following sample generates C3271:

// C3271.cpp  
// compile with: /clr /c  
using namespace System;  
using namespace System::Runtime::InteropServices;  
  
[StructLayout(LayoutKind::Explicit)]  
value class MyStruct1 {  
   public: [FieldOffset(0)] int a;  
   public: [FieldOffset(-1)] long b;   // C3271  
};  

The following sample generates C3271:

// C3271_2.cpp  
// compile with: /clr:oldSyntax  
using namespace System;  
using namespace System::Runtime::InteropServices;  
  
[StructLayout(LayoutKind::Explicit)]  
__value class MyStruct1 {  
   public: [FieldOffset(0)] int a;  
   public: [FieldOffset(-1)] long b;   // C3271  
};  
  
static int main() {  
   MyStruct1* a = __nogc new MyStruct1();  
};  

Show: