【问题标题】:Compiler error when trying to access public const field in C# assembly from C++/CLI尝试从 C++/CLI 访问 C# 程序集中的公共 const 字段时出现编译器错误
【发布时间】:2013-06-06 14:35:50
【问题描述】:

我有一个声明以下类的 C# 项目:

public static class CPConstants
{
    public const double FAR = double.PositiveInfinity;

    ...
}

我有一个单独的 C++/CLI 项目,它引用了上述 C# 程序集并尝试在类的方法中执行此操作:

double farValue = CPConstants::FAR;

当我编译它时(我在 Visual Studio 2008 中使用 MS Build),我得到以下编译器错误:

error C2589: ';' : illegal token on right side of '::'
error C2059: syntax error : '::'

谁能解释我在这里做错了什么?如何从我的 C++/CLI 项目中访问“FAR”字段?

【问题讨论】:

    标签: c# .net visual-studio interop c++-cli


    【解决方案1】:

    由于遗留原因,Windows 标头中有 #define FAR。尝试在标题包含和实际代码之间使用这一行:

    #undef FAR
    

    【讨论】:

    • 原来是这个原因。我想我只会在我的 C# 项目中重命名“FAR”变量,而不是执行“#undef”,但这个答案是准确的。谢谢!
    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 2020-06-26
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多