5

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…

Community
  • 1
  • 1
Bogey Jammer
  • 638
  • 2
  • 8
  • 23

4 Answers4

2

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();
    
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
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;
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.

Luke
  • 1,284
  • 1
  • 12
  • 34
  • I should use wcout for French writing. Unfortunately, none of your proposed solutions works for me… – Bogey Jammer Sep 06 '15 at 18:30
  • "'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`. – Tom Blodget Sep 07 '15 at 02:56
  • 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 used – Luke Sep 07 '15 at 10:31
  • (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) – Luke Sep 07 '15 at 10:35
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";
TDHofstetter
  • 268
  • 2
  • 7
  • It ends to be a string isn't it ? – Bogey Jammer Sep 06 '15 at 18:34
  • 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. – TDHofstetter Sep 06 '15 at 18:41
  • Thanks for the clarification. However, this still doesn't work – Bogey Jammer Sep 06 '15 at 18:43
  • 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.. – TDHofstetter Sep 06 '15 at 18:43
  • Try this: #include using namespace std; int main() { char* texte = "pouet"; cout << texte; } – TDHofstetter Sep 06 '15 at 18:48
  • Any progress? Sorry about the "Try this" one-line comment - I formatted it correctly, but the comment editor reformatted it into a single line. – TDHofstetter Sep 06 '15 at 19:05
  • See the question edit. Sorry guys to waste your time. Hopefully I still learned new things. – Bogey Jammer Sep 06 '15 at 19:12
  • 1
    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) – TDHofstetter Sep 07 '15 at 00:37