2013-02-20 144 views
3

我遇到问题。我需要将字符串类型转换为unicode。 我知道metod像在C++中将std :: string转换为Unicode字符串

string.c_str(); 

但它不起作用在我的代码。

我有功能

void modify(string infstring, string* poststring) 

,并在其中我需要显示备忘录infstring。像

Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text"); 

但是编译器说我“E2085无效指针除了”

我怎样才能解决我的问题?

+0

现在它说“不能将字符串转换为Unicode字符串”。我认为,那ss.str()返回字符串类型,但我需要在unicode字符串。有任何想法吗? – user2090826 2013-02-20 11:09:00

+1

你是什么意思“Unicode字符串”?这不是C++类型,你的意思是什么类型? – 2013-02-20 11:19:13

回答

4
Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text"); 

应该

Form1->Memo1->Lines->Add(("some text "+infstring+" some text").c_str()); 

即你的字符串文字添加到std::string然后使用c_str()从它那里得到一个const char*

如果Add()函数采用不同的类型,那么这仍然不起作用,但是您没有提供足够的信息来知道您在问什么。

+2

OP最有可能使用C++ Builder(语法与其VCL UI框架相匹配),在这种情况下,替代方案是:Form1-> Memo1-> Lines-> Add(“some text”+ String(infstring (),“infstring.length())+”some text“);'VCL的'String'类(大写'S'),Add()作为输入,接受'char *'和可选在它的构造函数中,并且可以与'char *'值连接来创建临时的'String'实例。 – 2013-02-20 18:22:02

2

使用字符串流

#include <sstream> 
std::stringstream ss; 
ss << "some text" << mystring << "some text"; 
Form1->Memo1->Lines->Add(ss.str().c_str());