为什么Visual Studio需要<string>编译,而代码块不需要?

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

本文关键字:代码 编译 不需要 string Studio Visual 需要 lt 为什么 gt      更新时间:2023-10-16

基本上,该程序是在代码块上编译的,而不是在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 *)'

所以问题是,为什么代码块可以编译和运行这个程序,但Visual Studio需要

#include <string>

我发现 - 感谢这个论坛 - 使用getline和<<运算符需要包含"包含字符串"行,但是为什么代码块可以在没有它的情况下工作,或者为什么Visual Studio 2015不能

编辑:是的,代码块正在使用GNU GCC编译器,VS2015正在使用默认编译器

允许(但不是必需)任何标准头文件包含任何其他头文件。

因此,在一个编译器上,您包含的标头之一确实包含<string>,而在另一个编译器上,它们都没有。

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