为什么Visual Studio需要<string>编译,而代码块不需要?
Why visual studio needs <string> to compile, but codeblocks doesnt?
基本上,该程序是在代码块上编译的,而不是在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>
,而在另一个编译器上,它们都没有。
这通常是棘手的(我的意思是很难做到正确,即使对于专家也是如此),但为了可移植性,恐怕您需要知道哪些标头包含您使用的声明,并确保包含所有声明。
相关文章:
- 如何修复sfml c++代码编译错误
- 尝试用java代码编译和运行c++代码
- 此代码编译良好,但文件未创建?请指出错误
- 为什么我的 BaseClass:Method 代码编译(带有单冒号)?
- 代码编译没有任何输出,入门程序
- 通过Python Distutils(用于Python C扩展)使用可重定位的设备代码编译CUDA代码
- 代码编译但不起作用!cmd窗口只是理想和理想,但什么也没发生
- 无法使用 g++ 使用C++代码编译 C 库
- 如何在Ubuntu中使用Visual Studio代码编译C++代码
- 推力+提升代码编译错误
- C++代码编译,但在 Zorin OS 上运行时显示错误
- 如何在使用模板时将 CPP 代码编译到库文件中
- 是否可以将Visual Studio 2017将C 代码编译到EXE以外的文件类型中
- 使用 Visual Studio for C++ 代码编译错误
- 使用 Android Studio 使用本机代码编译 apk 时,如何在链接处删除 libgnustl_static.
- 使用 Emscripten 将 OpenCV 代码编译C++ Javascript
- 将C 代码编译到独立应用程序.App
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 重用编译器前端的结果,以加快多个配置/平台的C++代码编译
- 如何将C++11代码编译为网络汇编