为什么 visual studio 需要 <string> 才能编译,而 codeblocks 不需要?

Why visual studio needs <string> to compile, but codeblocks doesnt?

基本上,program 是在代码块上编译,但不是在 visual studio 2015 上编译,除非我添加

#include <string>

到其中一个文件,然后我从代码的第一行得到错误

1>------ Build started: Project: ConsoleApplication2, Configuration: Release Win32 ------
1>  pytanie.cpp
1>pytanie.cpp(25): error C3861: 'getline': identifier not found
1>pytanie.cpp(42): error C2679: binary '<<': no operator found which takes a 
right-hand operand of type 'std::string' (or there is no acceptable 
conversion)

还有大约 200 行这样的东西

'std::basic_ostream<char,std::char_traits<char>>
&std::basic_ostream<char,std::char_traits<char>>::operator <<(const void *)'

那么问题来了,为什么codeblocks可以编译运行这个程序,但是visual studio需要

#include <string>

我发现 - 感谢这个论坛 - 使用 getline 和 << 运算符需要包含 'include string' 行,但为什么代码块没有它也能工作,或者为什么 visual studio 2015 不能

编辑:是的,codeblock 使用的是 GNU GCC 编译器,而 VS2015 使用的是默认编译器

允许但不要求任何标准 header 文件包含任何其他文件。

因此,在一个编译器上,您包含的 header 之一确实包含 <string>,而在另一个编译器上 none 包含

这通常很棘手(我的意思是即使是专家也很难做到正确),但为了可移植性,恐怕您需要知道哪些 header 包含您使用的声明,并确保包括所有这些。