Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple C++ program not compiling using wstrings and cout

I´m building this simple C++ program using Visual Studio 2012:

#include <stdafx.h>

#include <string>
#include <iostream>

int main()
{
    std::wcout << "Hello World...";

    std::string input_data;

    std::string output_data("Hello. Please type your name");

    std::wcout << output_data;
    std::wcin >> input_data;

    std::wcout << "Your name is " << input_data;

    return 0;
}

I can´t compile. Getting the followig errors:

error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::wistream' (or there is no acceptable conversion)

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

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

IntelliSense: no operator "<<" matches these operands
            operand types are: std::basic_ostream<wchar_t, std::char_traits<wchar_t>> << std::string    

IntelliSense: no operator "<<" matches these operands
            operand types are: std::wostream << std::string

IntelliSense: no operator ">>" matches these operands
            operand types are: std::wistream >> std::string

Can someone help me to fix that ?

like image 472
Mendes Avatar asked Sep 10 '26 12:09

Mendes


1 Answers

You should try changing all std::string ocurrences for std::wstring... or all wcin/wcout for cin/cout... (in the first case, prefix the strings like L"aaa" This, for instance, works perfectly:

#include <string>
#include <iostream>

int main()
{
    std::wcout << L"Hello World...";

    std::wstring input_data;

    std::wstring output_data(L"Hello. Please type your name");

    std::wcout << output_data;
    std::wcin >> input_data;

    std::wcout << L"Your name is " << input_data;

    return 0;
}
like image 177
Massa Avatar answered Sep 13 '26 03:09

Massa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!