0

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.

codeLover
  • 3,623
  • 10
  • 58
  • 115
  • I`m assuming you are looking for `w_str = arr;`, but there are other problems with your code also. – Jesse Good Jan 30 '12 at 08:27
  • Yes somehow I want to convert tchar[256] array into a string object (wstring or string), so that I will be able to use the functions like substr() etc.. in wstring or string. – codeLover Jan 30 '12 at 08:49

2 Answers2

0

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.

Some programmer dude
  • 380,411
  • 33
  • 383
  • 585
0

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.

Community
  • 1
  • 1
Cody Gray
  • 230,875
  • 49
  • 477
  • 553
  • The problem in declaring as above is that, w_str is a global variable which would get populated inside a function & TCHAR arr[256] is a local array variable inside this function. SO HOW CAN I INITIALIZE w_str(arr) WITH arr. ? – codeLover Jan 30 '12 at 07:56