gpt4 book ai didi

c++ - 为什么 visual studio 需要 来编译,而 codeblocks 不需要?

转载 作者:行者123 更新时间:2023-11-28 02:10:58 28 4
gpt4 key购买 nike

基本上,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 之一确实包含 <string> ,而在其他编译器上,它们都没有。

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

关于c++ - 为什么 visual studio 需要 <string> 来编译,而 codeblocks 不需要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35687516/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com