Solved

std::wstring wont work with overloaded std::operator<<

Posted on 2002-04-15
2
1,415 Views
Last Modified: 2013-12-14
hello experts,

can anyone tell me why this code compiles and runs:
---------------------------------------------------
#include <string>
#include <iostream>

using namespace std ;

void main()
{
    string msg="Hello!";
    cout << msg << endl;
}
-------------------------------------------------

whereas this code (using std::wstring instead of std::string)
-------------------------------------------------
#include <string>
#include <iostream>

using namespace std ;

void main()
{
    wstring msg=L"Hello";
    cout << msg << endl;
}
-------------------------------------------------
produces the following error:

error C2679 binary '<<' : no operator found which takes a right-hand operand of type 'std::wstring' (or there is no acceptable conversion)
?

string, wstring (calsses) and operator<< (function) are all templates defined in <string>.
what do i have to do in order to be able to use operator<< on types templated on std::basic_string other than std::string?

thanks!
0
Comment
Question by:managair
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6942035
Because "cout" has no "operator << (const wstring&);" - use "wcout" instead, e.g.

include <string>
#include <iostream>

using namespace std ;

void main()
{
   wstring msg=L"Hello";
   wcout << msg << endl;
}

See also "iostream", it has mutliple objects for handling UNICODE:

IOSTREAM:extern _CRTIMP ostream cout; // ANSI
IOSTREAM:extern _CRTIMP wostream wcout, wcerr, wclog; // UNICODE


0
 

Author Comment

by:managair
ID: 6942073
it makes sense that if i use a wstring i should use a wostream.

thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question