C++/CLI:公共引用结构生成 C2011:“类"类型重新定义 [英] C++/CLI: Public ref struct generates C2011: 'class' type redefinition

查看:26
本文介绍了C++/CLI:公共引用结构生成 C2011:“类"类型重新定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在托管 DLL 项目中有一个头文件,如下所示:

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

枚举.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":class"类型重新定义.

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.

非常感谢您的任何见解!

Thanks very much for any insight!

推荐答案

当您使用托管代码时,您必须稍微调整一下传统的 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 once 不能解决这个问题.

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:公共引用结构生成 C2011:“类"类型重新定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆