How do I print a wstring?

  • Thread starter Thread starter UnixWolf
  • Start date Start date
U

UnixWolf

Guest
I'm working with an old commercial library that returns wstrings from some functions. Unfortunately, the code breaks with error "C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::wstring' (or there is no acceptable conversion)".

I tossed together a little program to demonstrate the error:

----snip--------snip--------snip----

1 #include <iostream>
2 #include <string>

3 using std::wstring;
4 using std::cout;

5 wstring
6 world()
7 {
8 wstring whirled(L"whirled!");
9 return whirled;
10 }

11 int main()
12 {
13 cout << L"hello, ";
14 cout << world();
15
16 return 0;

17 }

----snip--------snip--------snip----

However, when I use strings instead of wstrings, the program compiles and runs.

----snip--------snip--------snip----

1 #include <iostream>
2 #include <string>

3 using std::string;
4 using std::cout;

5 string
6 world()
7 {
8 string whirled("whirled!");
9 return whirled;
10 }

11 int main()
12 {
13 cout << "hello, ";
14 cout << world();
15
16 return 0;
17 }

----snip--------snip--------snip----

What blindingly obvious fact am I missing?

It's an old library, and seems to upset recent releases of Visual Studio. I'm using VS2008 at for this particular project.

Continue reading...
 
Back
Top