C++/CLI: Public ref struct generates C2011: 'class' type redefinition C++/CLI: Public ref struct generates C2011: 'class' type redefinition windows windows

C++/CLI: Public ref struct generates C2011: 'class' type redefinition


You have to de-tune the traditional C/C++ header file think a bit when you work with managed code. The principal source of type declarations is the assembly metadata. This is very different from the native C/C++ compilation model where you have to have a header file for types that you make visible to other modules.

I'm going to guess that you get this C2011 error in the EXE project. Where you both added a reference to the DLL project assembly (like you should) and used #include on the header file. Like you should not. That's a guaranteed duplicate definition, #pragma once doesn't fix that.

Don't use header files for exported type definitions. Always use assembly references.


I Know this question is a bit old, but I'm writing this for future usage:I had the following problem, which was similar:managed DLL had a managed class.managed.h:

 namespace Managed {            ref class CManagedClass {...}    }

in an unamanged class I wanted to use this above class and so in unmanaged.h

#include "managed.h"

in another DLL I also did:

#include "unmanged.h"

which resolved in the type redefinition error.I have found a solution to this issue using the following method:forward declaration in the unmanaged.h

namespace Managed {    ref class CManagedClass;}

and include the managed.h in the unmanaged.cpp file as usual.