c++ - 不使用 wstrings 和 cout 编译的简单 C++ 程序

标签 c++ visual-studio-2012 c++11

我使用 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::string 出现的 std::wstring... 或所有 wcin/wcout for cin/cout...(在第一种情况下,在字符串前加上 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;
}

关于c++ - 不使用 wstrings 和 cout 编译的简单 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23258401/

相关文章:

c# - 确保控制台项目中的 app.config 复制到 Web 项目的 bin 文件夹

c++ - 从线程中获取 native 句柄?

c++ - 字符串转换的段错误?

c++ - 尝试为屏幕的每个像素着色时出现线条?

c++ - "\x"在字符串中如何工作?

C++标准头文件

c# - 在 C# 中修改现有的注册表项值

.net - 如何确定执行期间来自异步 httpclient 的并发请求数?

c++ - `const &&` 是否绑定(bind)到所有 prvalues(和 xvalues)?

c++ - 将枚举作为函数参数传递