【问题标题】:Convert standard C++ int to String^将标准 C++ int 转换为 String^
【发布时间】:2018-03-30 12:49:32
【问题描述】:

我想在 Visual C++/CLI 环境中将 std::int 转换为 System::String^。我知道我们可以使用微软提供的包含模板函数 marshal_as 的库

#include <msclr\marshal.h>

using namespace msclr::interop;

int count = 1;
String^ Apple;
Apple = marshal_as<String^>(count);

由于出现此错误,我无法运行此语句

'msclr::interop::error_reporting_helper<_To_Type,_From_Type,false>::marshal_as':
 This conversion is not supported by the library or the header file needed for 
this conversion is not included.

【问题讨论】:

  • String^ 不是 C++ 中的有效类型。你是说 C++/CLI 吗?
  • 是的 C++/CLI。感谢指正

标签: c++-cli


【解决方案1】:
int count = 100; 
System::String^ s = count.ToString();

【讨论】:

    【解决方案2】:

    marshal_as 用于在本机类型与其托管等效项之间进行转换。它不是通用的“将任何东西转换为任何东西”工具。要将整数转换为 .NET String,您可以使用 std::to_string 转换为 C++ 字符串,然后将 marshal_as 转换为 String,或者如果您想在 .NET 中进行转换,请使用 Int.ToString - 侧面。

    【讨论】:

      猜你喜欢
      • 2016-11-29
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多