簡體   English   中英

簡單的C ++程序沒有使用wstrings和cout進行編譯

[英]Simple C++ program not compiling using wstrings and cout

我正在使用Visual Studio 2012構建這個簡單的C ++程序:

#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;
}

我無法編譯。 得到以下錯誤:

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

有人可以幫我解決這個問題嗎?

您應該嘗試更改std::wstring所有std::string事件...或者cin / cout所有wcin / wcout ...(在第一種情況下,字符串的前綴如L"aaa"這樣,例如,有效完美:

#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;
}

或者,您可以將所有內容切換為窄字符串:

#include <string>
#include <iostream>

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

    std::string input_data;

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

    std::cout << output_data;
    std::cin >> input_data;

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

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM