C ++ / CLI:Public ref struct生成C2011:'class'type redefinition [英] C++/CLI: Public ref struct generates C2011: 'class' type redefinition
问题描述
我在托管的DLL项目中有一个头文件:
I have a header file in a managed DLL project like so:
Enums.h:
#pragma once
...
public ref struct ManagedStruct {
Bitmap^ image;
}
...
DLL和从单独的可执行文件。单独的托管结构正在生成:
This header is referenced both from another class in the DLL and from a separate executable. The managed struct alone is generating:
错误C2011:'ManagedStruct':'类'类型重新定义
error C2011: 'ManagedStruct' : 'class' type redefinition.
如果我将结构移动到DLL中的主头文件,它工作正常,可以公开访问,这就是我在做什么,但我会非常喜欢了解为什么这种情况发生时,我只是把它移动到另一个文件。
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 the error.
非常感谢任何洞察力。
推荐答案
您必须调整传统的C / C ++头文件,当您使用托管代码时,有点想。类型声明的主要来源是组件元数据。这与本地C / C ++编译模型非常不同,在本地C / C ++编译模型中,您可以具有一个头文件,用于其他模块可见的类型。
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.
我要猜测你在EXE项目中得到这个C2011错误。你在哪里都添加了一个对DLL项目组件的引用(就像你应该)和在头文件中使用#include。像你不应该。这是一个保证重复的定义,#pragma一次不能解决这个问题。
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.
这篇关于C ++ / CLI:Public ref struct生成C2011:'class'type redefinition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!