【问题标题】:Compile time errors while working with C++/CLI examples使用 C++/CLI 示例时出现编译时错误
【发布时间】:2013-08-23 18:12:36
【问题描述】:

我正在尝试从文件中读取一个“数字”字符串,并将其转换回整数。以下是我的代码,即 C++/CLI

int InformationReader::getThreshold()
{
    StreamReader ^reader = gcnew StreamReader("threshold.dat");
    System::String ^thresholdStr = reader->ReadLine();

    Int32 thresholdNum;

    boolean a = Int32::TryParse(thresholdStr,thresholdNum);

    return 0;

}

但是,一旦执行此代码,我就会收到以下错误

1>InformationReader.cpp(29): error C2065: 'Int32' : undeclared identifier
1>InformationReader.cpp(29): error C2146: syntax error : missing ';' before identifier 'thresholdNum'
1>InformationReader.cpp(29): error C2065: 'thresholdNum' : undeclared identifier
1>InformationReader.cpp(31): error C2065: 'boolean' : undeclared identifier
1>InformationReader.cpp(31): error C2146: syntax error : missing ';' before identifier 'a'
1>InformationReader.cpp(31): error C2065: 'a' : undeclared identifier
1>InformationReader.cpp(31): error C2653: 'Int32' : is not a class or namespace name
1>InformationReader.cpp(31): error C2065: 'thresholdNum' : undeclared identifier
1>InformationReader.cpp(31): error C3861: 'TryParse': identifier not found

好的,现在这对我来说很陌生,因为我经历了许多问题和答案,并且在所有这些问题中,他们都遵循了与我使用过的类似的方法,但我遇到了错误。为什么是这样?

【问题讨论】:

  • 我不是 C++ 专家,所以想知道如何在 C++ 中指定 out 参数,例如 C# 示例 Int32.TryParse("2", out i);。只是好奇,因为 TryParse 的全部目的不是抛出异常。
  • 你掉进了没有他们的工具就编译 sn-ps 的陷阱。
  • 缺少常见的using namespace System; 指令。布尔值应大写或为布尔值。
  • @HansPassant:'bool' 的东西,很酷。我纠正了它。非常感谢您的帮助。

标签: .net visual-studio-2010 visual-c++ c++-cli


【解决方案1】:

修复你看到的第一个编译错误:你需要在Int32 thresholdNum;前面加上System::

【讨论】:

  • 或者,您可以在两个地方都使用int 代替Int32(是的,int::TryParse 也应该可以使用)。
  • 非常感谢您的帮助。
  • @Medinoc:是的,我现在做到了。非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2018-01-15
  • 2021-11-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多