简体   繁体   中英

Getting build error while assigning (converting) a TCHAr array to a std::string object

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.

You are trying to call the string objects as function (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM