Search code examples
stringvisual-c++visual-c++-2008c2664

error c2664 upon using basic_string<wchar_t> abc("hello") in vc++ 9.0


Hi upon compiling this code in Visual studio 2008 i get the following error

#include<iostream>
#include<string>
using namespace std;
void main()
{
     basic_string<wchar_t> abc("hello world");
     cout<<abc;
     return;
}

error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'const char [12]' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it'

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

what is that i'm doing wrong?

Can any one help me understand the things happening behind? Thanks


Solution

  • wchar_t specifies wide character types. By default a const char pointer to a literal string is not wide, but you can tell the compiler to treat it as a wide character array by prefixing it with 'L'.

    So just change to

    basic_string<wchar_t> abc(L"hello world");