将 String^ 转换为 std::string (基本字符串) -> 错误。 我怎样才能解决这个问题?

发布于 2024-07-17 20:07:56 字数 575 浏览 5 评论 0原文

我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

会傲 2024-07-24 20:07:57

也许,您需要反向转换,从 std::stringString^
我正在搜索这个转换,但没有找到。 所以,我写了。 我希望,这对某人有用:

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;
}    

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:

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;
}    
坚持沉默 2024-07-24 20:07:57

我认为您也可以这样做:

string dummy = string( textBox1->Text );

查看如何: MSDN 上的各种字符串类型之间的转换

I think you can also just do:

string dummy = string( textBox1->Text );

Check out How to: Convert Between Various String Types on MSDN.

坦然微笑 2024-07-24 20:07:57
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;
}
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;
}
别把无礼当个性 2024-07-24 20:07:56

您需要整理您的字符串。 托管字符串位于托管堆上的某个位置(垃圾收集器可以自由移动它)。

将字符串传送到本机端的一种方法如下:

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

如果您使用的是 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));

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.

#include <msclr/marshal.h>
#include <msclr/marshal_cppstd.h>

using namespace msclr::interop;

std::string nativeString(marshal_as<std::string>(managedString));
忆梦 2024-07-24 20:07:56

我花了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;

I spend 11h to find a solution:

 #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;
野鹿林 2024-07-24 20:07:56

您需要做两件事才能将 System::String 转换为 std::string

  • 将托管堆中的内存封送到非托管堆中。
  • 将您的字符编码从宽字符转换为(从您的问题来看)ansi 字符。

一种无需担心释放任何 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..
}

您可以像这样使用它:

// 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
}

这也允许您使用您选择的编码(例如 utf-8 over ascii),但是你可能并不真正需要这个。

You need to do two things to convert a System::String to a 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.

One way, without having to worry about freeing any HGlobal memory is to define a method along the lines of:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文