Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

722 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