将 String^ 转换为 std::string (基本字符串) -> 错误。 我怎样才能解决这个问题?
我尝试将 String^ 转换为基本字符串...但在这段代码之后出现错误。 这是什么意思以及如何解决它? 我需要将基本字符串输入到类构造函数中。 字符串^ 不起作用。
System::String^ temp = textBox1->Text;
string dummy = System::Convert::ToString(temp);
error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::basic_string' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits, 1> _Ax=std::allocator 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous
I try to convert a String^ to basic string...but I get the error after this code. What does it mean and how can I fix it? I need to input basic string into a class constructor. The string^ would not work.
System::String^ temp = textBox1->Text;
string dummy = System::Convert::ToString(temp);
error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::basic_string' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits, 1> _Ax=std::allocator 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许,您需要反向转换,从
std::string
到String^
。我正在搜索这个转换,但没有找到。 所以,我写了。 我希望,这对某人有用:
May be, you will need inverse converting, from
std::string
toString^
.I was searching this converting, but I didn't find. So, I wrote it. I hope, it will be useful for somebody:
我认为您也可以这样做:
查看如何: MSDN 上的各种字符串类型之间的转换。
I think you can also just do:
Check out How to: Convert Between Various String Types on MSDN.
您需要整理您的字符串。 托管字符串位于托管堆上的某个位置(垃圾收集器可以自由移动它)。
将字符串传送到本机端的一种方法如下:
如果您使用的是 Visual Studio 2008,则可以利用更好的封送实用程序。 查看此 MSDN 页面。
You need to marshal your string. The managed string sits somewhere on the managed heap (garbage collector is free to move it around).
One way of getting the string to the native side is as follows:
If you are using Visual Studio 2008, you can take advantage of much nicer marshaling utilities. Check out this MSDN page.
我花了11个小时才找到解决方案:
I spend 11h to find a solution:
您需要做两件事才能将
System::String
转换为std::string
:一种无需担心释放任何 HGlobal 内存的方法是定义一个方法,如下所示:
您可以像这样使用它:
这也允许您使用您选择的编码(例如 utf-8 over ascii),但是你可能并不真正需要这个。
You need to do two things to convert a
System::String
to astd::string
:One way, without having to worry about freeing any HGlobal memory is to define a method along the lines of:
And you'd use it like:
This also lets you use the encoding of your choice (like utf-8 over ascii), but you may not really need this.