windows C++/CLI:公共引用结构生成 C2011:“类”类型重新定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2977534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++/CLI: Public ref struct generates C2011: 'class' type redefinition
提问by T3db0t
I have a header file in a managed DLL project like so:
我在托管 DLL 项目中有一个头文件,如下所示:
Enums.h:
枚举.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:
该头文件从 DLL 中的另一个类和一个单独的可执行文件中引用。单独的托管结构正在生成:
error C2011: 'ManagedStruct' : 'class' type redefinition.
错误 C2011:“ManagedStruct”:“class”类型重新定义。
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.
如果我将结构移动到 DLL 中的主头文件,它就可以正常工作,并且可以公开访问,这就是我正在做的事情,但是我非常想知道当我将它移动到另一个文件时为什么会发生这种情况.
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!
非常感谢您的任何见解!
回答by Hans Passant
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 haveto have a header file for types that you make visible to other modules.
当您使用托管代码时,您必须稍微调整一下传统的 C/C++ 头文件。类型声明的主要来源是程序集元数据。这与本地 C/C++ 编译模型非常不同,在本地 C/C++ 编译模型中,您必须拥有一个对其他模块可见的类型的头文件。
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) andused #include on the header file. Like you should not. That's a guaranteed duplicate definition, #pragma once doesn't fix that.
我猜你会在 EXE 项目中遇到这个 C2011 错误。您在其中添加了对 DLL 项目程序集的引用(就像您应该的那样)并在头文件上使用了 #include。就像你不应该。这是一个有保证的重复定义,#pragma once 不能解决这个问题。
Don't use header files for exported type definitions. Always use assembly references.
不要将头文件用于导出的类型定义。始终使用程序集引用。
回答by Harel M
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:
我知道这个问题有点老了,但我写这个是为了将来使用:我有以下类似的问题:托管 DLL 有一个托管类。托管.h:
namespace Managed {
ref class CManagedClass {...}
}
in an unamanged class I wanted to use this above class and so in unmanaged.h
在一个无人管理的课程中,我想在上面的课程中使用这个,所以在 unmanaged.h 中
#include "managed.h"
in another DLL I also did:
在另一个 DLL 中我也做了:
#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
解决了类型重定义错误。我使用以下方法找到了解决此问题的方法:unmanaged.h 中的前向声明
namespace Managed {
ref class CManagedClass;
}
and include the managed.h in the unmanaged.cpp file as usual.
并像往常一样在 unmanaged.cpp 文件中包含 managed.h。