避免递归(?)#include声明

dar*_*rio 2 c++ header

我在c ++项目中使用自定义库,包括几个标题头,但是当我在主文件中包含相应的标题时,就像我在自定义文件中包含了所有标题.
要明确:
自定义头文件:

#ifndef CUSTOM_H
#define CUSTOM_H

#include <vector>

//stuff

#endif
Run Code Online (Sandbox Code Playgroud)

Main.cpp的:

#include <iostream>
#include "custom.h"

//here, let suppose that i do next:
vector<int> vec;

return 0;
Run Code Online (Sandbox Code Playgroud)

没有编译错误,像包含矢量标题,我想避免,任何建议

phs*_*phs 5

如果custom.h使用的std::vector是实现细节(并且未在标题中定义的内容的签名中公开),请考虑移动#include <vector>到相应的custom.cpp文件.

如果您无法移动包含,custom.h因为,例如,您将一个向量传入或传出方法,那么它确实是您的界面的一部分,并且标题的客户端需要知道它.