我使用 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/