【问题标题】:How to make a public variable from a "public ref class" in c++?如何从 C++ 中的“公共引用类”创建公共变量?
【发布时间】:2012-05-11 11:59:59
【问题描述】:

如何允许变量处于“公共引用类”的公共/全局范围内?
视觉 C++ [2010]
在我的主 DLL main.h 中:

namespace fdll {

 public ref class foo
 {
   public:
     int bar(int num);
  };

}

在我的 DLL main.cpp 中:

#include "main.h"
int fdll::foo::bar(int num)
{
  return num;
}


在另一个项目中:

#using <main.dll>
#include "main.h"
fdll::foo f; <--- error wtf
int main()
{
  Console::WriteLine(fdll.bar(2));
  return 0;
}


给出的错误:
错误 C3145:“f”:全局或静态变量可能没有托管类型“fdll::foo”
不得声明全局或静态变量,或引用 gc 堆中对象的本机类型的成员

【问题讨论】:

    标签: c++ class visual-c++ public ref


    【解决方案1】:

    CLR 对象不支持全局变量,如 here 所述。

    创建一个全局类,使用static模拟全局变量或者只在函数范围内声明CLR变量。

    【讨论】:

    • 非常感谢!尽管该链接很有帮助,但我还是决定将我的 CLR/DLL 更改为静态库项目以帮助解决我的问题。虽然这不是我的真实代码,但我举了一个广泛的例子来寻求帮助。
    猜你喜欢
    • 2022-11-14
    • 2017-04-10
    • 1970-01-01
    • 2014-02-17
    • 2020-03-27
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多