0

I know we can insulate a class by using pointer so that the header of the class is not required in header, e.g.:

class B;
class A{
    B* b;
};

It prevents #include "B.h" in A.h. Now I want to prevent

#include <vector>

in header, so I try to copy the syntax, use a pointer of vector:

class std::vector<B*>;
class A{
    std::vector<B*>* v;
};

but it failed to compile, is it possible to prevent include vector header by using pointer of vector?

ggrr
  • 7,737
  • 5
  • 31
  • 53

2 Answers2

3

You're looking for:

#include <vector>

class B;
class A
{
    std::vector<B *> *v;
};

It's not permitted to attempt to provide your own declarations for members of namespace std;

If you do not want your header to include <vector> then you cannot use any type involving vector in your class. You could use a pImpl instead.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
0

No need of doing class std::vector; define class A as

#include <vector>
class B;
class A
{
  std::vector<B*> *v; 
};

Or you can typedef std::vector<B*> VEC_PTR_B_T; and use VEC_PTR_B_T *v in class A.

SEB
  • 21
  • 1