c++生成器、label.caption、std::string 到 unicode 转换
只需要设置 lbl.caption (在循环内),但问题比我想象的要大。我什至尝试过使用 wstrings 向量,但没有这样的东西。我读过一些页面,尝试过一些函数,如 WideString()、UnicodeString(),我知道我不能也不应该在 C++Builder 2010 中关闭 Unicode。
std::vector <std::string> myStringVec(20, "");
myStringVec.at(0) = "SomeText";
std::string s = "something";
// this works ..
Form2->lblTxtPytanie1->Caption = "someSimpleText";
// both lines gives the same err
Form2->lblTxtPytanie1->Caption = myStringVec.at(0);
Form2->lblTxtPytanie1->Caption = s;
Err: [BCC32 Error] myFile.cpp(129 ): E2034 无法将 'std::string' 转换为 'UnicodeString'
它已经吃了我几个小时了。有没有“快速而肮脏”的解决方案?它只需要工作...
更新
已解决。我混合了 STL / VCL 字符串类。谢谢你TommyA。
Just need to set the lbl.caption (inside a loop) but the problem is bigger than i thought. I've tried even with vector of wstrings but there is no such thing. I've read some pages, tried some functions like WideString(), UnicodeString(), i know i can't and shouldn't turn off Unicode in C++Builder 2010.
std::vector <std::string> myStringVec(20, "");
myStringVec.at(0) = "SomeText";
std::string s = "something";
// this works ..
Form2->lblTxtPytanie1->Caption = "someSimpleText";
// both lines gives the same err
Form2->lblTxtPytanie1->Caption = myStringVec.at(0);
Form2->lblTxtPytanie1->Caption = s;
Err: [BCC32 Error] myFile.cpp(129): E2034 Cannot convert 'std::string' to 'UnicodeString'
It ate me few hours now. Is there any "quick & dirty" solution ? It just has to work...
UPDATE
Solved. I've mixed STL / VCL string classes. Thank You TommyA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您将 标准模板库字符串类 与 VCL 字符串类。标题属性需要 VCL 字符串,它具有 STL 字符串的所有功能。
有效的示例实际上是传递 (
const char*
),这很好,因为 VCLUnicodeString
类构造函数中有一个构造函数,但是没有构造函数用于从 STL 字符串复制。您可以执行以下两件事之一,您可以在向量中使用 VCL 字符串类之一而不是 STL 字符串类,这样:
变为:
在这种情况下,底部两行也将起作用。或者,您可以从 STL 字符串中检索实际的以空结尾的字符指针,并将它们传递给标题,此时它将转换为 VCL String 类,如下所示:
您更喜欢哪种解决方案取决于您,但除非您有一些解决方案对于 STL 字符串类的具体需求,我强烈建议您使用 VCL 字符串类(如我在第一个示例中所示)。这样您就不必拥有两个不同的字符串类。
The problem is that you are mixing standard template library string class with the VCL string class. The caption property expects the VCL string which has all the functionality of the STL one.
The example that works is really passing (
const char*
) which is fine because there is a constructor for this in the VCLUnicodeString
class constructor, however there isn't a constructor for copying from STL strings.You could do one of two things, you could use one of the VCL string classes in your vector instead of the STL ones, so that:
Becomes:
In which case the bottom two lines will also work. Alternatively you can retrieve the actual null terminated character pointer from the STL strings and pass them to the caption, at which point it will be converted into a VCL String class like this:
Which solution you prefer is up to you, but unless you have some specific need for the STL string class I would strongly suggest you going with the VCL string classes (as I showed in my first example). This way you won't have to have two different string classes.