将 TCHAr 数组分配(转换)到 std::string 对象时出现构建错误
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您试图将字符串对象作为函数调用(即调用
operator()
),但是std::basic_string
(从中std::string
> 和std::wstring
是类型定义的)没有该运算符。相反,您应该在构造字符串时进行初始化:但是,我认为最后一次构造时仍然会出现错误,因为
std::string
和std::wstring
使用不同的字符类型。You are trying to call the string objects as function (i.e. invoking
operator()
), butstd::basic_string
(from whichstd::string
andstd::wstring
is typedefed) doesn't have that operator. Instead you should do the initialization when constructing the strings:However, I think you will still get an error with the last construction, because
std::string
andstd::wstring
uses different character types.我假设您正在尝试调用
std::wstring
和std::string
的构造函数,但您的做法是错误的。相反,在声明时初始化对象:但这并不能解决所有问题。您无法直接从 Unicode(宽)字符串转换为窄字符串。您将必须进行某种转换。请参阅此问题了解可能的解决方案。
不过,要求这个确实很奇怪。选择一种字符串类型并坚持使用。如果您正在对 Windows 进行编程并因此使用 Unicode 字符串,则您希望始终使用
std::wstring
。I assume that you're trying to call the constructor for
std::wstring
andstd::string
, but you're doing it the wrong way. Instead, initialize the objects at the time of declaration: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.