Solved

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

Posted on 2002-04-15
2
1,384 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
Comment Utility
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
Comment Utility
it makes sense that if i use a wstring i should use a wostream.

thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now