I can't output a string with wcout
Asked Answered
V

4

7

It's a sort of duplicate of this question. I followed the recommendations (I think) and included that <string> but the exact same error is thrown at my face :

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

#include <string>
#include <iostream>

using namespace std;

int main() {
    string texte;
    texte = "pouet";
    wcout << texte << endl;
    return 0;
}

EDIT: I not proud at all to say the issue was caused by the fact I did not selected the correct project as the "starting project". Visual Studio is kinda hard to apprehend… However, the initial real issue concerned my real project, and was about standard string that cannot be output via wcout. I reformatted the question to re-orient the subject accordingly. Downvote me as you wish, I deserve it…

Vedis answered 6/9, 2015 at 18:11 Comment(4)
You might find this relevant: #14697497Vacuva
"using cout doesn't help"… o.O?Laundromat
Let's stop the downvoting.Uxorial
See also: https://mcmap.net/q/240188/-how-to-initialize-and-print-a-std-wstring (and my comment)Beaumarchais
V
1

This compiles and runs fine on my CLion with latest MinGW

#include <string>
#include <iostream>

using namespace std;

int main() {
    string texte;
    texte = "pouet";
    cout << texte << endl;
    return 0;
}

For wcout, aka wide strings, this should provide correct output:

#include <string>
#include <iostream>

using namespace std;

int main() {
    wstring texte;
    texte = L"pouet";
    wcout << texte << endl;
    return 0;
}

cout outputs 'regular' strings with characters that are 1 byte wide (usually ASCII), while wcout is made for 'wide' strings which are composed of characters whose representation takes up more than 1 byte.

Vacuva answered 6/9, 2015 at 18:23 Comment(4)
I should use wcout for French writing. Unfortunately, none of your proposed solutions works for me…Vedis
"'regular' strings with characters that are 1 byte wide (usually ASCII)." Programs that are limited to ASCII are not common at all. Almost certainly not on systems that support wcout.Hairsplitting
Right, but string is an array of chars, and char is defined to have CHAR_BIT bits (required to be 8 in POSIX-compilant systems), so 'regular' chars are in the most cases 8 bits wide. In practice, those 8 bytes be encoded using ASCII or any compatible encoding for first 128 (you can't really say UTF-8 or any Unicode encoding in general is different than ASCII for the first 128 chars), with possibility to extend it for another 128 chars. Considering UTF-8 is de facto default encoding for most things, network esp, such 'extensions' would be quite risky and from my experience aren't so widely usedVacuva
(wide chars/strings and wcout are different story altogether, and imho should be used in any program that is meant to be translated one day)Vacuva
D
4

The output operator isn't overloaded for std::basic_string to operate for arbitrary character types for streams. Your options are:

  • Create a std::wstring from your std::string, e.g.:

    std::wcout << std::wstring(texte.begin(), texte.end());
    
  • Since the output operators are overloaded for C-style strings even if the character type doesn't match you can just get a character array:

    std::wcout << texte.c_str();
    
Dramatize answered 6/9, 2015 at 18:25 Comment(0)
M
1

if you use wcout you have to use wstring too and you have to put an 'L' in front of your const strings.

wstring texte;
texte = L"pouet";
wcout << texte << endl;
Mehalek answered 6/9, 2015 at 18:21 Comment(0)
V
1

This compiles and runs fine on my CLion with latest MinGW

#include <string>
#include <iostream>

using namespace std;

int main() {
    string texte;
    texte = "pouet";
    cout << texte << endl;
    return 0;
}

For wcout, aka wide strings, this should provide correct output:

#include <string>
#include <iostream>

using namespace std;

int main() {
    wstring texte;
    texte = L"pouet";
    wcout << texte << endl;
    return 0;
}

cout outputs 'regular' strings with characters that are 1 byte wide (usually ASCII), while wcout is made for 'wide' strings which are composed of characters whose representation takes up more than 1 byte.

Vacuva answered 6/9, 2015 at 18:23 Comment(4)
I should use wcout for French writing. Unfortunately, none of your proposed solutions works for me…Vedis
"'regular' strings with characters that are 1 byte wide (usually ASCII)." Programs that are limited to ASCII are not common at all. Almost certainly not on systems that support wcout.Hairsplitting
Right, but string is an array of chars, and char is defined to have CHAR_BIT bits (required to be 8 in POSIX-compilant systems), so 'regular' chars are in the most cases 8 bits wide. In practice, those 8 bytes be encoded using ASCII or any compatible encoding for first 128 (you can't really say UTF-8 or any Unicode encoding in general is different than ASCII for the first 128 chars), with possibility to extend it for another 128 chars. Considering UTF-8 is de facto default encoding for most things, network esp, such 'extensions' would be quite risky and from my experience aren't so widely usedVacuva
(wide chars/strings and wcout are different story altogether, and imho should be used in any program that is meant to be translated one day)Vacuva
H
0

Are you married to the string type? You can do what you're trying to do by making texte a char* instead of a string:

char* texte = "pouet";
Horsehide answered 6/9, 2015 at 18:24 Comment(8)
It ends to be a string isn't it ?Vedis
A "string" is an object with methods, built around char primitives. A char array (in the case of my suggestion, a constant array pointed to by the char*) is a "string" in a very different sense - it's not an object, it has no methods; it's just a bare storage place in which to store data.Horsehide
Thanks for the clarification. However, this still doesn't workVedis
If you use the "string" TYPE, then your compiler will use the object. If you use char[...] or char* as your type, then the compiler will use an array of chars, which is often referred to as a "string" but isn't the same thing as the "string" type..Horsehide
Try this: #include <iostream> using namespace std; int main() { char* texte = "pouet"; cout << texte; }Horsehide
Any progress? Sorry about the "Try this" one-line comment - I formatted it correctly, but the comment editor reformatted it into a single line.Horsehide
See the question edit. Sorry guys to waste your time. Hopefully I still learned new things.Vedis
You haven't wasted our time - you've fed us stuff to think about. I can't speak for anyone else, but I'm certainly not going to vote anything down here! 8)Horsehide

© 2022 - 2024 — McMap. All rights reserved.