我在"Rad Studio 10 Seattle"中遇到问题,我试图从TEdit
对象获取文本输入,但收到一条错误消息,说
E2034无法将"UnicodeString"转换为"string"
我的代码如下:
this->shopVar->addDog(edName->Text, StrToInt(edAge->Text), "male", "dogspecial");
此函数addDog
采用(string, int, string, string)
当我尝试用edName->Text
从TEdit
对象发送文本时,我会收到前面提到的错误消息。
我的问题如下,我可以在edName
上从unicodestring转换为string吗?还是必须更改参数列表的数据类型?如果是,我该怎么做
我一直在寻找这个问题,但没有发现任何与我的问题类似的东西。
您需要先将UnicodeString
数据转换为Ansi,然后再传递。最简单的方法是先将UnicodeString
分配给AnsiString
(或导数),然后使用其c_str()
方法获得char*
指针,std::string
将接受该指针:
this->shopVar->addDog(AnsiString(edName->Text).c_str(), ...);
如果可能,可以考虑重写shopVar
类以使用std::wstring
,然后可以删除Ansi转换:
this->shopVar->addDog(edName->Text.c_str(), StrToInt(edAge->Text), L"male", L"dogspecial");