Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-04-15
2
Medium Priority
?
1,522 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

885 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