将 TCHAr 数组分配(转换)到 std::string 对象时出现构建错误

发布于 2024-12-29 14:23:25 字数 497 浏览 4 评论 0原文

我正在使用 unicode 字符集(一般要求也是仅使用 unicode)。 我想以某种方式将 TCHAr 数组的内容放入 std::string obj 中,以便我可以使用该 obj 的函数。 我的代码片段如下:

TCHAR arr[256];
std::wstring w_str;
std::string s_str;

w_str(arr);  ---> Error 1
s_str(w_str.begin,w_str.end);  ---> Error 2. 

错误 1:我收到错误 C2064:“Term 的计算结果不是 函数采用 1 个参数。

错误 2:我收到错误 C2064:“术语的计算结果不为 一个带有 2 个参数的函数。

任何人都可以在这方面帮助我吗?让我知道如何将 TCHAR(使用 unicode 字符集)的内容分配给字符串对象。

I am using unicode character set(Generic requirement is too use unicode only).
I wnat to somehow place the contents of TCHAr array into a std::string obj, so that I can use the functions of this obj.
My code snippet is as follows:

TCHAR arr[256];
std::wstring w_str;
std::string s_str;

w_str(arr);  ---> Error 1
s_str(w_str.begin,w_str.end);  ---> Error 2. 

Error 1 : I am gettin the error C2064: "Term does not evaluate to a
function taking 1 parameter.

Error 2: I am gettin the error C2064: "Term does not evaluate to
a function taking 2 parameter.

Can anyone kindly help me in this; let me know how to assign contents of a TCHAR (using unicode char set), to a string object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

帅气尐潴 2025-01-05 14:23:25

您试图将字符串对象作为函数调用(即调用 operator()),但是 std::basic_string (从中 std::string > 和 std::wstring 是类型定义的)没有该运算符。相反,您应该在构造字符串时进行初始化:

std::wstring w_str(arr);
std::string s_str(w_str.begin(), w_str.end());

但是,我认为最后一次构造时仍然会出现错误,因为 std::stringstd::wstring使用不同的字符类型。

You are trying to call the string objects as function (i.e. invoking operator()), but std::basic_string (from which std::string and std::wstring is typedefed) doesn't have that operator. Instead you should do the initialization when constructing the strings:

std::wstring w_str(arr);
std::string s_str(w_str.begin(), w_str.end());

However, I think you will still get an error with the last construction, because std::string and std::wstring uses different character types.

这样的小城市 2025-01-05 14:23:25

我假设您正在尝试调用 std::wstringstd::string 的构造函数,但您的做法是错误的。相反,在声明时初始化对象:

TCHAR arr[256];
std::wstring w_str(arr);
std::string s_str(w_str.begin(), w_str.end());

但这并不能解决所有问题。您无法直接从 Unicode(宽)字符串转换为窄字符串。您将必须进行某种转换。请参阅此问题了解可能的解决方案。

不过,要求这个确实很奇怪。选择一种字符串类型并坚持使用。如果您正在对 Windows 进行编程并因此使用 Unicode 字符串,则您希望始终使用 std::wstring

I assume that you're trying to call the constructor for std::wstring and std::string, but you're doing it the wrong way. Instead, initialize the objects at the time of declaration:

TCHAR arr[256];
std::wstring w_str(arr);
std::string s_str(w_str.begin(), w_str.end());

That's not going to solve all of your problems, though. You can't convert directly from a Unicode (wide) string to a narrow string. You're going to have to do some sort of conversion. See this question for possible solutions.

It's strange to require this at all, though. Pick a string type and stick with it. If you're programming Windows and therefore using Unicode strings, you want to use std::wstring throughout.

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