Reputation: 57
I'm using a custom library in a c++ project, witch includes several std headers, but when i include the corresponding header in the main file, it's like i included all the headers in the custom one.
To be clear:
custom header file:
#ifndef CUSTOM_H
#define CUSTOM_H
#include <vector>
//stuff
#endif
Main.cpp:
#include <iostream>
#include "custom.h"
//here, let suppose that i do next:
vector<int> vec;
return 0;
there's no compile error, like the vector header is included, i want to avoid that, any suggestion
Upvotes: 2
Views: 2273
Reputation: 308216
The problem is nearly unavoidable. If your header file needs to include another for any reason whatsoever, there's no way to "uninclude" it later.
There's one technique that can minimize the problem, the pimpl idiom or opaque pointer. By moving the implementation of a class into a private source file, you eliminate the dependencies that caused you to include the header in the first place.
Upvotes: 0
Reputation: 75130
It is possible, but this shouldn't be done without consideration because it gives meaning to the order in which you include files, that is, you will or won't be able to compile depending on what order your #includes are in. It is generally desirable to avoid that situation.
To accomplish that, remove #include <vector>
from the .h and add it to Main.cpp above where you include the custom header file. If you #include it below that, it will complain about types being undefined. Also in every file that uses the custom header file, they will have to #include <vector>
first.
Upvotes: 0
Reputation: 11051
If custom.h
's use of std::vector
is an implementation detail (and not exposed in the signatures of things you define in the header), consider moving the #include <vector>
to the corresponding custom.cpp
file instead.
If you can't move the include out of custom.h
because, say, you pass a vector in or out of a method, then it truly is part of your interface and clients of your header will need to know about it.
Upvotes: 5