是否需要显式地包含其他标头已经包含它们自己的标头?
Do you need to explicitly include headers that other headers already include themselves?
给定头文件foo.h
:
#include <bar>
// ...
和文件baz.cpp
:
#include "foo.h"
// ...
你需要明确地包括bar
头到baz.cpp
为了使用它?或者你可以开始使用它,因为它包含在foo.h
中?
如果main.cpp
使用了<algorithm>
中定义的任何函数或类,则需要将#include <algorithm>
添加到main.cpp
中。
其他翻译单元使用什么是不相关的
不,你不需要。可以把"#include"看作是复制和粘贴所包含文件的全部内容的指示。
other.h:
#include <string>
#include <vector>
std::string getString()
{
return "A String";
}
main.cpp:
#include <iostream>
#include "other.h"
int main()
{
std::vector<std::string> vec{getString(), getString()};
for (auto &it : vec) {
std::cout << it << std::endl;
}
return 0;
}
相关文章:
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- 如何正确包含我自己的标头?
- 包含它的模板化类型的shared_ptr
- 为什么存储在数组中的地址值与其自己的地址相同,它应该指向某个不同的地址(&arr[0])?
- 当包含头文件的文件不在根项目文件夹中时,如何包含它
- 在wxWidgets的事件中包含我自己的数据的最佳方法是什么?
- 访问另一个类(系统)的非静态字段,就好像它是我自己的字段一样 - 优雅地
- 对象文件中的未定义引用 - 如何查找哪个库包含它
- 如何在自己的仅标头库中包含提升标头
- .c文件应该包含它的.h文件已经包含的内容吗?
- 如何将另一个项目中用 C 编写的源代码包含在我自己的项目中,C++在 Visual Studio 中
- 当包含它的对象调用其析构函数时,unique_ptr是否未分配
- G++ 切换为不包含自己的符号函数名称(和调试数据)-
- 是否可以使用TensorFlow训练自己的模块,然后在OpenCV中使用它进行检测
- 张量类,它有自己的存储,但也可以映射外部指针
- C 程序要读取包含两个双重值的Excel数据的单列,这些值由逗号分隔并存储在自己的数组中
- MinGW g++ 在自己的包含目录中找不到标头
- 应用程序如何在c++中删除自己和包含它的文件夹
- 在它自己的源文件中包含头文件