C++ 错误 C2679:二进制“>>”:未找到采用“std::string”类型的右侧操作数的运算符(或没有可接受的转换)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1631338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:39:33  来源:igfitidea点击:

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

c++visual-c++compiler-errorscygwin

提问by asyncwait

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:

Visual C++ Compiler (VS2008) 中的以下代码未编译,而是引发此异常:

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?

但是这段代码在 cygwin g++ 中编译得很好。有什么想法吗?

回答by sbi

Have you included all of the following headers?

您是否包含以下所有标题?

  • <fstream>
  • <istream>
  • <iostream>
  • <string>
  • <fstream>
  • <istream>
  • <iostream>
  • <string>

My guess is you forgot <string>.

我猜你忘了<string>

On a side note: That should be std::coutand std::endl.

附带说明:那应该是std::coutstd::endl

回答by Guy Avraham

Adding to @sbianswer, in my case the difference was including <string>instead of <string.h>(under VS 2017).

添加到@sbi答案中,在我的情况下,差异是包括<string>而不是<string.h>(在 VS 2017 下)。

See the following answer: similar case answer

见以下答案:类似案例答案

回答by Nick Delbar

In addition to what others said. The following code was necessary in my application to compile succesfully.

除了别人说的。我的应用程序需要以下代码才能成功编译。

std::cout << s.c_str() << std::endl;

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)

另一个解决方法是转到项目属性 -> 常规 -> 字符集并选择“Ues 多字节字符集”(您不需要使用 c_str() 来输出字符串)

There's disadvantages to using MBCS so if you plan to localize your software, I'd advize against this.

使用 MBCS 有一些缺点,因此如果您打算本地化您的软件,我建议您不要这样做。

回答by Akshat Bhatt

include <string>

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.

尝试在<iostream>文件中包含字符串头文件。即使没有,它也可以在某些编译器中工作,<string>因为不同编译器的设置不同,并且编译器负责读取以“#”符号开头的预处理器文件以生成 obj 文件。