【问题标题】:how to declare public class如何声明公共类
【发布时间】:2012-04-29 23:01:21
【问题描述】:

对于这个听起来如此愚蠢的问题,我感到非常抱歉:)

我有这样一行代码:

namespace Messages{
/// @brief Interface to support building a message during decoding.
class ValueMessageBuilder : public Common::Logger
....

由于这个错误,我不能在我的 c# 项目中使用ValueMessageBuilder

Cannot access internal struct 'ValueMessageBuilder' here.

所以我尝试将其公开并重新编译 dll:

public class ValueMessageBuilder : public Common::Logger

但是编译失败,出现这样的错误Error C3381: 'QuickFAST::Messages::ValueMessageBuilder' : assembly access specifiers are only available in code compiled with a /clr option F:\Oleg\quickfast_1_4_0_my\src\Messages\ValueMessageBuilder.h 17 1 QuickF‌​AST

那么问题是如何将内部ValueMessageBuilder结构转换为public?

【问题讨论】:

标签: .net c++-cli


【解决方案1】:

由于错误状态,您的代码必须使用 /clr 选项(项目属性 | 常规 | 公共语言运行时支持)进行编译。之后,您的项目将变为 c++/clr(托管 c++)。 另外我认为该类应该以 ref 关键字开头,以便在 c# 中可见。

【讨论】:

  • /clr 已设置,ref 关键字不起作用(见上面的评论)
【解决方案2】:

要使类在 C# 中可用,它必须是具有 .NET 元数据的托管类型。

使用ref classvalue class

请注意,一个对象不能同时包含托管类和本机类类型。但是,本地类可以持有托管实例的句柄(使用gcroot),托管类型可以持有指向本地对象的指针(My smart pointer, posted on codereview, may help with lifetime management)。

【讨论】:

  • 使用ref classvalue class 我收到this declaration has no storage class or type specified 错误
  • @javapowered:听起来/clr 并没有在包含此标头的每个文件上设置。
  • 但我在项目属性中看到/clr 选项,我不知道我还能做什么...
  • @javapowered:项目有编译选项,也有每个单独编译单元的编译选项(相同的属性窗口,从.cpp文件访问)。
  • 检查项目中所有可能的文件并不容易
猜你喜欢
  • 2016-06-06
  • 2014-12-03
  • 1970-01-01
  • 2010-12-06
  • 2015-03-27
  • 2017-04-24
  • 2018-10-29
  • 2011-10-11
  • 2017-05-15
相关资源
最近更新 更多