Reputation: 1465
I've created a class A and a Class B, and I'm trying to set a vector of type B in A, and a vector of type A in B:
class A Header:
#ifndef A_H_
#define A_H_
#include "B.h"
#include <vector>
using namespace std;
class A {
public:
vector<B> bvector; // here is the error
A();
};
#endif /* A_H_ */
class B header:
#ifndef B_H_
#define B_H_
#include "A.h"
#include <vector>
using namespace std;
class B {
vector<A> aVector; //Here is the error
public:
B();
};
#endif /* B_H_ */
But I get the following errors:
"..\src/B.h:16:8: error: 'A' was not declared in this scope
..\src/B.h:16:9: error: template argument 1 is invalid
..\src/B.h:16:9: error: template argument 2 is invalid"
Which flips to A.h
if I delete the bad line in B. What am I doing wrong?
Upvotes: 4
Views: 309
Reputation: 39224
The trick here is that you need to forward declare one of the two classes. Unfortunately, forward declarations won't let you use the full type in the header - they'll only allow you to use a pointer to the type. So even that wouldn't work in this case.
You can use the PIMPL idiom (Pointer to IMPLementation) (google that) to circumvent this issue.
The PIMPL idiom would basically have you create a third type. Then your type B would hold a pointer to an implementation class that contained your vector of type A. All operations of the vector that was supposed to be held in B would be forwarded to the new implementation class which actually held the Vector of A.
For example:
//Forward declare impl class.
class BImpl;
class B {
private:
BImpl* impl;
public:
void PrintVector() { impl->PrintVector(); }
};
class A {
private:
std::vector<B> vec;
public:
void PrintVector() { /* Do printing */ }
};
class BImpl {
private:
std::vector<A> vec;
public:
void PrintVector() { /* Do Printing */ }
};
I haven't included the constructor or population methods for those classes, but you should get the general idea :)
Upvotes: 1
Reputation: 39164
ive created a class A and a Class B, im trying to set a vector of type B in A, and a vector of type A in B
You are creating a circular dependency between your classes. This is generally a bad thing, particularly in C++.
The compiler, in order to compile A, needs to know the definition of B (#include "B.h"). Unfortunately the B header contains a reference to A class (here's the circular reference). The compiler can't handle this situation, since A header is already included in the current TU (see include guards).
Despite the fact that often having circular references is a symptom of a bad design, you can eventually overcome the problem using forward declaration. For example you could modify B this way:
#ifndef B_H_
#define B_H_
#include <vector>
using namespace std;
class A; //forward declaration of class A
class B {
vector<A*> aVector; //note that you must only use pointer to A now
public:
B();
};
#endif /* B_H_ */
Using a forward declaration basically tells the compiler that a type A will be defined somewhere else. The compiler can rely on this fact, but it does not know anything about A (particularly it ignores the size of A and its methods). So inside B, if you have forward declared A, you can use only pointers to A classes (pointers have always the same size) and you can't call any methods of an A class from inside B.
Upvotes: 2
Reputation: 171253
You have a circular dependency.
A.h
includes B.h
which includes A.h
, but the A_H_
guard is already defined so the contents of A.h
are skipped, so the contents of B.h
are processed, which refers to A
but it's not defined yet because you haven't processed anything in A.h
except the #define
and #include
directives at the top of A.h
Upvotes: 0