C++/CLI:公共引用结构生成 C2011:'class'类型重定义
我在托管 DLL 项目中有一个头文件,如下所示:
Enums.h:
#pragma once
...
public ref struct ManagedStruct {
Bitmap^ image;
}
...
此头文件既从 DLL 中的另一个类又从单独的可执行文件引用。仅托管结构正在生成:
错误 C2011:“ManagedStruct”:“class”类型重新定义。
如果我将结构移动到 DLL 中的主头文件,它可以正常工作,并且可以公开访问,所以这就是我正在做的事情,但我非常想了解当我将其移动到另一个文件时为什么会发生这种情况。
我已经检查了所有必要的包含和命名空间,并尝试了明显的标头防护,但无济于事;我仍然收到错误。
非常感谢您的任何见解!
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 the error.
Thanks very much for any insight!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用托管代码时,您必须稍微调整一下传统的 C/C++ 头文件。类型声明的主要来源是程序集元数据。这与本机 C/C++ 编译模型非常不同,在本机 C/C++ 编译模型中,您必须拥有一个对其他模块可见的类型的头文件。
我猜测您在 EXE 项目中遇到了此 C2011 错误。您添加了对 DLL 项目程序集的引用(就像您应该的那样),并且在头文件上使用了 #include。就像你不应该那样。这是一个有保证的重复定义,#pragma Once 不能解决这个问题。
不要将头文件用于导出的类型定义。始终使用程序集引用。
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.
我知道这个问题有点老了,但我写这个是为了将来使用:
我遇到了以下类似的问题:
托管 DLL 有一个托管类。
Managed.h:
在一个未修改的类中,我想使用上面的类,因此在
另一个 DLL 的 unmanagement.h 中我也这样做了:
这解决了类型重定义错误。
我使用以下方法找到了解决此问题的方法:
像往常一样,在 unmanaged.h 中进行前向声明
,并将 Managed.h 包含在 unmanagement.cpp 文件中。
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:
in an unamanged class I wanted to use this above class and so in unmanaged.h
in another DLL I also did:
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
and include the managed.h in the unmanaged.cpp file as usual.