Solved

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

Posted on 2002-04-15
2
1,445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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