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.
我尝试将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
6 个解决方案
#1
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:
将字符串传递给本机端的一种方法如下:
using System::Runtime::InteropServices::Marshal;
char *pString = (char*)Marshal::StringToHGlobalAnsi(managedString);
std::string nativeString(pString); // make your std::string
Marshal::FreeHGlobal(pString); // don't forget to clean up
If you are using Visual Studio 2008, you can take advantage of much nicer marshaling utilities. Check out this MSDN page.
如果您使用的是Visual Studio 2008,则可以利用更好的编组实用程序。查看此MSDN页面。
#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>
using namespace msclr::interop;
std::string nativeString(marshal_as<std::string>(managedString));
#2
I spend 11h to find a solution:
我花了11个小时才找到解决方案:
#include <stdlib.h
#include <string.h>
#include <msclr\marshal_cppstd.h>
//ListaQuad<int> prueba;
//..
using namespace msclr::interop;
//..
System::String^ clrString = (TextoDeBoton);
std::string stdString = marshal_as<std::string>(clrString); //String^ to std
//System::String^ myString = marshal_as<System::String^>(MyBasicStirng); //std to String^ (no lo he probado, pero sería algo así.)
prueba.CopyInfo(stdString); //MyMethod
//..
//where, String^ = TextoDeBoton;
//and, stdString = normal string;
#3
You need to do two things to convert a System::String
to a std::string
:
您需要做两件事来将System :: String转换为std :: string:
- Marshal the memory from the managed heap an unmanaged one.
- Convert your character encoding from a wide characters to (what looks like from your question) ansi characters.
从托管堆中分配内存和非托管堆。
将您的角色编码从宽字符转换为(从您的问题看起来像)ansi字符。
One way, without having to worry about freeing any HGlobal memory is to define a method along the lines of:
一种方法,无需担心释放任何HGlobal内存,就是定义一种方法:
interior_ptr<unsigned char> GetAsciiString(System::String ^s)
{
array<unsigned char> ^chars = System::Text::Encoding::ASCII->GetBytes(s);
return &chars[0];
// of course you'd want to do some error checking for string length, nulls, etc..
}
And you'd use it like:
你会像以下一样使用它:
// somewhere else...
{
pin_ptr<unsigned char> pChars = GetAsciiString(textBox1->Text);
std:string std_str(pChars); // I don't have my compiler in front of me, so you may need a (char*)pChars
}
This also lets you use the encoding of your choice (like utf-8 over ascii), but you may not really need this.
这也允许您使用您选择的编码(如ascii上的utf-8),但您可能不需要这样。
#4
May be, you will need inverse converting, from std::string
to String^
. I was searching this converting, but I didn't find. So, I wrote it. I hope, it will be useful for somebody:
可能是,你需要逆转换,从std :: string到String ^。我正在寻找这种转换,但我没有找到。所以,我写了它。我希望,这对某些人有用:
String^ stdStringToPlatformString(string stdString){
const size_t newsizew = strlen(stdString.c_str()) + 1;
size_t convertedChars = 0;
wchar_t *ch1 = new wchar_t[newsizew];
mbstowcs_s(&convertedChars, ch1, newsizew, stdString.c_str(), _TRUNCATE);
String ^platformString;
platformString = ref new Platform::String(ch1, newsizew);
return platformString;
}
#5
I think you can also just do:
我想你也可以这样做:
string dummy = string( textBox1->Text );
Check out How to: Convert Between Various String Types on MSDN.
请查看如何:在MSDN上转换各种字符串类型。
#6
string platformStringToStdString(String ^input){
int size=input->Length()+1;
char* auxiliary=(char *)malloc(sizeof(char)*size);
wcstombs_s(NULL,auxiliary,size,input->Data(),_TRUNCATE);
string result(auxiliary);
free(auxiliary);
return result;
}
#1
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:
将字符串传递给本机端的一种方法如下:
using System::Runtime::InteropServices::Marshal;
char *pString = (char*)Marshal::StringToHGlobalAnsi(managedString);
std::string nativeString(pString); // make your std::string
Marshal::FreeHGlobal(pString); // don't forget to clean up
If you are using Visual Studio 2008, you can take advantage of much nicer marshaling utilities. Check out this MSDN page.
如果您使用的是Visual Studio 2008,则可以利用更好的编组实用程序。查看此MSDN页面。
#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>
using namespace msclr::interop;
std::string nativeString(marshal_as<std::string>(managedString));
#2
I spend 11h to find a solution:
我花了11个小时才找到解决方案:
#include <stdlib.h
#include <string.h>
#include <msclr\marshal_cppstd.h>
//ListaQuad<int> prueba;
//..
using namespace msclr::interop;
//..
System::String^ clrString = (TextoDeBoton);
std::string stdString = marshal_as<std::string>(clrString); //String^ to std
//System::String^ myString = marshal_as<System::String^>(MyBasicStirng); //std to String^ (no lo he probado, pero sería algo así.)
prueba.CopyInfo(stdString); //MyMethod
//..
//where, String^ = TextoDeBoton;
//and, stdString = normal string;
#3
You need to do two things to convert a System::String
to a std::string
:
您需要做两件事来将System :: String转换为std :: string:
- Marshal the memory from the managed heap an unmanaged one.
- Convert your character encoding from a wide characters to (what looks like from your question) ansi characters.
从托管堆中分配内存和非托管堆。
将您的角色编码从宽字符转换为(从您的问题看起来像)ansi字符。
One way, without having to worry about freeing any HGlobal memory is to define a method along the lines of:
一种方法,无需担心释放任何HGlobal内存,就是定义一种方法:
interior_ptr<unsigned char> GetAsciiString(System::String ^s)
{
array<unsigned char> ^chars = System::Text::Encoding::ASCII->GetBytes(s);
return &chars[0];
// of course you'd want to do some error checking for string length, nulls, etc..
}
And you'd use it like:
你会像以下一样使用它:
// somewhere else...
{
pin_ptr<unsigned char> pChars = GetAsciiString(textBox1->Text);
std:string std_str(pChars); // I don't have my compiler in front of me, so you may need a (char*)pChars
}
This also lets you use the encoding of your choice (like utf-8 over ascii), but you may not really need this.
这也允许您使用您选择的编码(如ascii上的utf-8),但您可能不需要这样。
#4
May be, you will need inverse converting, from std::string
to String^
. I was searching this converting, but I didn't find. So, I wrote it. I hope, it will be useful for somebody:
可能是,你需要逆转换,从std :: string到String ^。我正在寻找这种转换,但我没有找到。所以,我写了它。我希望,这对某些人有用:
String^ stdStringToPlatformString(string stdString){
const size_t newsizew = strlen(stdString.c_str()) + 1;
size_t convertedChars = 0;
wchar_t *ch1 = new wchar_t[newsizew];
mbstowcs_s(&convertedChars, ch1, newsizew, stdString.c_str(), _TRUNCATE);
String ^platformString;
platformString = ref new Platform::String(ch1, newsizew);
return platformString;
}
#5
I think you can also just do:
我想你也可以这样做:
string dummy = string( textBox1->Text );
Check out How to: Convert Between Various String Types on MSDN.
请查看如何:在MSDN上转换各种字符串类型。
#6
string platformStringToStdString(String ^input){
int size=input->Length()+1;
char* auxiliary=(char *)malloc(sizeof(char)*size);
wcstombs_s(NULL,auxiliary,size,input->Data(),_TRUNCATE);
string result(auxiliary);
free(auxiliary);
return result;
}