我正在用 C++/CLI 构建一个项目,我必须在其中以我的一种形式显示消息框。
内容必须是 std::string 和 int 的组合。
但我无法获得正确的语法。
我尝试了以下方法:
std::string stringPart = "ABC";
int intPart = 10;
MessageBox::Show("Message" + stringPart + intPart);
我也试过:
String^ msg = String::Concat("Message", stringPart);
msg = String::Concat(msg, intPart);
MessageBox::Show(msg);
谁能帮我解决一下语法问题。
谢谢。
最佳答案
你的问题是 std::string
是非托管的,不能分配给托管的 System::String
。解决方案是编码。请参阅此 MSDN 页面:http://msdn.microsoft.com/en-us/library/bb384865.aspx
这是解决方案(适用于 Visual Studio):
#include <msclr/marshal_cppstd.h>
// ...
std::string stringPart = "ABC";
int intPart = 10;
String^ msg = String::Concat("Message", msclr::interop::marshal_as<System::String^>(stringPart));
msg = String::Concat(msg, intPart);
MessageBox::Show(msg);
关于winforms - MessageBox::Show 中需要 C++/CLI 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5771989/