2013-02-20 86 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()); 
相關問題