错误 C2679:二进制 '>>' : 未找到采用“std::string”类型右侧操作数的运算符(或者没有可接受的转换)
请不要与标题混淆,因为有人已经问过它,但上下文不同
Visual C++ 编译器 (VS2008) 中的以下代码不会被编译,而是抛出此异常
std::ifstream input (fileName);
while (input) {
string s;
input >> s;
std::cout << s << std::endl;
};
:代码在 cygwin g++ 中编译良好。有什么想法吗?
Please don't confuse with the title as it was already asked by someone but for a different context
The below code in Visual C++ Compiler (VS2008) does not get compiled, instead it throws this exception:
std::ifstream input (fileName);
while (input) {
string s;
input >> s;
std::cout << s << std::endl;
};
But this code compiles fine in cygwin g++. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否已包含以下所有标头?
我猜你忘了<代码><字符串>。
附带说明:这应该是
std::cout
和std::endl
。Have you included all of the following headers?
<fstream>
<istream>
<iostream>
<string>
My guess is you forgot
<string>
.On a side note: That should be
std::cout
andstd::endl
.添加到 @sbi 答案,在我的例子中,区别在于包括< /code>(在 VS 2017 下)。
而不是请参阅以下答案:类似案例答案
Adding to @sbi answer, in my case the difference was including
<string>
instead of<string.h>
(under VS 2017).See the following answer: similar case answer
include
尝试包含字符串头文件和
文件。即使没有
,它也可以在某些编译器中工作,因为不同编译器的设置不同,并且编译器负责读取以“#”符号开头的预处理器文件以生成.obj 文件。include <string>
Try including string header file along with
<iostream>
file.It will work in some compilers even without the
<string>
because settings for different compilers are different and it is the compiler that is responsible for reading the preprocessor files that start with '#' symbol to generate a obj file.除了别人说的。我的应用程序需要以下代码才能成功编译。
另一个解决方法是转到项目属性 ->一般->字符集并选择“使用多字节字符集”(您不需要使用 c_str() 来输出字符串)
使用 MBCS 有缺点,因此如果您打算本地化您的软件,我建议不要这样做。
In addition to what others said. The following code was necessary in my application to compile succesfully.
Another work-around to this is go to project properties -> General -> Character Set and choose "Ues Multi-Byte Character Set" (You won't need to use c_str() to output the string)
There's disadvantages to using MBCS so if you plan to localize your software, I'd advize against this.