c++生成器、label.caption、std::string 到 unicode 转换

发布于 2024-08-18 15:38:05 字数 734 浏览 6 评论 0原文

只需要设置 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 技术交流群。

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

发布评论

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

评论(1

扎心 2024-08-25 15:38:05

问题是您将 标准模板库字符串类VCL 字符串类。标题属性需要 VCL 字符串,它具有 STL 字符串的所有功能。

有效的示例实际上是传递 (const char*),这很好,因为 VCL UnicodeString 类构造函数中有一个构造函数,但是没有构造函数用于从 STL 字符串复制。

您可以执行以下两件事之一,您可以在向量中使用 VCL 字符串类之一而不是 STL 字符串类,这样:

std::vector <std::string> myStringVec(20, "");
myStringVec.at(0) = "SomeText";
std::string s = "something";

变为:

std::vector <String> myStringVec(20, "");
myStringVec.at(0) = "SomeText";
String s = "something";

在这种情况下,底部两行也将起作用。或者,您可以从 STL 字符串中检索实际的以空结尾的字符指针,并将它们传递给标题,此时它将转换为 VCL String 类,如下所示:

// both lines will now work
Form2->lblTxtPytanie1->Caption = myStringVec.at(0).c_str();
Form2->lblTxtPytanie1->Caption = s.c_str();

您更喜欢哪种解决方案取决于您,但除非您有一些解决方案对于 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 VCL UnicodeString 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:

std::vector <std::string> myStringVec(20, "");
myStringVec.at(0) = "SomeText";
std::string s = "something";

Becomes:

std::vector <String> myStringVec(20, "");
myStringVec.at(0) = "SomeText";
String s = "something";

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:

// both lines will now work
Form2->lblTxtPytanie1->Caption = myStringVec.at(0).c_str();
Form2->lblTxtPytanie1->Caption = s.c_str();

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.

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