Compiler Error C3270

 

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 C3270 on docs.microsoft.com.

'field': the FieldOffset attribute can only be used in the context of StructLayout(Explicit), in which case it is required

A field was marked with FieldOffset, which is only allowed when StructLayout Explicit is in effect.

The following sample generates C3270:

// C3270_2.cpp  
// compile with: /clr /c  
using namespace System::Runtime::InteropServices;  
  
[ StructLayout(LayoutKind::Sequential) ]  
// try the following line instead  
// [ StructLayout(LayoutKind::Explicit) ]  
public value struct MYUNION  
{  
   [FieldOffset(0)] int a;   // C3270  
   // ...  
};  

The following sample generates C3270:

// C3270.cpp  
// compile with: /clr:oldSyntax /LD  
#using <mscorlib.dll>  
using namespace System::Runtime::InteropServices;  
  
[ StructLayout(LayoutKind::Sequential) ]  
// try the following line instead  
// [ StructLayout(LayoutKind::Explicit) ]  
public __value struct MYUNION  
{  
   [FieldOffset(0)] int a;   // C3270  
   // ...  
};  

Show: