我想避免这种情况:
h1.hpp:
#include <vector>
//some code
main.cpp中:
#include "h1.hpp"
//#include <vector> This include is commented.
int main(){
std::vector<int> x; //works fine!
}
我希望收到错误/警告我正在使用<vector>
,但它并未直接包含在MSVS中。
答案 0 :(得分:-1)
如果将<vector>
用作实现细节(即函数体),请将h1.hpp与h1_head.hpp(声明)和h1_body.hpp(定义)分开。如果您有结构/类,则可以通过以下方式隐藏详细信息:在类中声明detail
结构/类,只在h1_body.hpp中定义它。在main.cpp中的代码之前包含h1_head.hpp,在所有代码之后包含h2_body.hpp 。
如果h1.hpp中的任何一个函数接受/返回一个向量或相关类型,那么在包含h1时,最好假设main.cpp 将 <vector>
。另一种方法是说h1 需要一个向量类,因此在h1中不包含<vector>
,但要求它包含在main.cpp中(在h1之前) - 但是非常混乱,因为没有编程方式来记录它(不,要求typedef std::vector vector_t;
或#define H1VECTOR std::vector
不记录,这是错误的概括。)