Include order issues
-
Hi, I'm working with a large project with lots of classes and I have a problem with including the classes. I use include guards at the top of my .h files (I've tried using #pragma once as well) to prevent including more than once. When I include the header of a class I need to use I get an error saying "undefined class", if I was using a pointer I could use a forward declaration but I'm not using a pointer, how do I get around this? Thanks.
-
Hi, I'm working with a large project with lots of classes and I have a problem with including the classes. I use include guards at the top of my .h files (I've tried using #pragma once as well) to prevent including more than once. When I include the header of a class I need to use I get an error saying "undefined class", if I was using a pointer I could use a forward declaration but I'm not using a pointer, how do I get around this? Thanks.
It'd help if you posted some code... But if you have something like this:
// a.h
#include "b.h"
class A
{
// Stuff
B b;
};// b.h
#include "a.h"
class B
{
// Stuff
A a;
};then you're stuffed - that can't be fixed, because A needs to see a full definition of B before it's defined, and B needs to see a full definition of A before it's defined.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
It'd help if you posted some code... But if you have something like this:
// a.h
#include "b.h"
class A
{
// Stuff
B b;
};// b.h
#include "a.h"
class B
{
// Stuff
A a;
};then you're stuffed - that can't be fixed, because A needs to see a full definition of B before it's defined, and B needs to see a full definition of A before it's defined.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thanks, that makes sense. I hadn't looked at it that simply until I saw that. What would be the "standard" way of creating member variables that reference each other, using your example:
// a.h
#include "b.h"
class B;
class A
{
// Stuff
B *b;CString s1;
};// b.h
#include "a.h"
class A;
class B
{
// Stuff
A *a;CString s2;
};That would work now I've used a forward declaration of the class and used a pointer but it seems "messy" to me to have one class as a pointer and the other not. It also seems a waste to have to implement a destructor to clean up the pointers.
-
Thanks, that makes sense. I hadn't looked at it that simply until I saw that. What would be the "standard" way of creating member variables that reference each other, using your example:
// a.h
#include "b.h"
class B;
class A
{
// Stuff
B *b;CString s1;
};// b.h
#include "a.h"
class A;
class B
{
// Stuff
A *a;CString s2;
};That would work now I've used a forward declaration of the class and used a pointer but it seems "messy" to me to have one class as a pointer and the other not. It also seems a waste to have to implement a destructor to clean up the pointers.
Pointers are good. An alternative is to use the pimpl idiom[^]:
// a.h
class A
{
A();
// Stuff
private:
struct A_impl;
A_impl* impl_;
};// b.h
class B
{
B();
// Stuff
private:
struct B_impl;
B_impl* impl_;
};// a.cpp
#include "a.h"
#include "b.h"
struct A::A_impl
{
B b;
};A::A() : impl_(new A_impl) {}
// b.cpp
#include "b.h"
#include "a.h"
struct B::B_impl
{
A a;
};B::B() : impl_(new B_impl) {}
In either case, you need to take care with copy constructors and assignment operators.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Pointers are good. An alternative is to use the pimpl idiom[^]:
// a.h
class A
{
A();
// Stuff
private:
struct A_impl;
A_impl* impl_;
};// b.h
class B
{
B();
// Stuff
private:
struct B_impl;
B_impl* impl_;
};// a.cpp
#include "a.h"
#include "b.h"
struct A::A_impl
{
B b;
};A::A() : impl_(new A_impl) {}
// b.cpp
#include "b.h"
#include "a.h"
struct B::B_impl
{
A a;
};B::B() : impl_(new B_impl) {}
In either case, you need to take care with copy constructors and assignment operators.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p