views:

207

answers:

1

Greetings! I have a header file in a managed DLL project like so:

Enums.h:

#pragma once
...
public ref struct ManagedStruct {
    Bitmap^ image;
}
...

This header is referenced both from another class in the DLL and from a separate executable. The managed struct alone is generating error C2011: 'ManagedStruct' : 'class' type redefinition. If I move the struct to the main header file in the DLL it works fine, and is publicly accessible, so that's what I'm doing, but I would very much like to learn why this is happening when I just move it to another file.

I have checked all necessary includes and namespaces AND tried the obvious header guards, to no avail; I still get error C2011: 'ManagedStruct' : 'class' type redefinition.

Thanks very much for any insight!

+3  A: 

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.

Hans Passant
You are correct—thanks for the information! Just when I thought I was getting the hang of C++/CLI...
Virgil Disgr4ce