Circular includes and forward declarations

Hi, so I'm having a bit of an issue, and I know it has to do with the fact that I have circular includes, but it seems somewhat necessary. Here's essentially what I have:

1
2
3
4
5
6
7
8
// Base.h
#pragma once

#include "SpecificChild.h"

class Base {
SpecificChild* mySpecificChild;
};

1
2
3
4
5
6
7
// SpecificChild.h
#pragma once

#include "Base.h"

class SpecificChild : public Base {
};


The issue is that when Base.cpp gets compiled, it includes Base.h. Base.h in turn includes SpecificChild.h. SpecificChild.h tries to include Base.h but sees nothing because of the #pragma once (essentially an #ifdef _BASE_H ... #endif for those not familiar with MSVC). I have had this issue before when I was dealing with two different, unrelated classes, and that was fixed by putting a simple forward declaration in each header file. However, putting "class Base;" above SpecificChild's declaration (and putting "class SpecificChild;" above Base's declaration just in case), still does not help. I get the following error:
"'Base' : base class undefined"

Obviously, and easy and cheap solution to this would be just to change the reference to SpecificChild in Base to just a Base, but I would like to use stuff specific to SpecificChild, and it seems pointless to cast it every time I want to do so.

Is there some specific way of forwardly declaring a class that you intend to use as a base? Thanks.
Last edited on
You can declare a class, but not if that is going to be a base class as the compiler needs to know what the members should be.
Your problem could be solved with just one forward declaration:

1
2
3
4
5
6
7
8
// Base.h
#pragma once

class SpecificChild;

class Base {
SpecificChild* mySpecificChild;//no need for full implementation of SpecificChild
};

1
2
3
4
5
6
7
// SpecificChild.h
#pragma once

#include "Base.h"

class SpecificChild : public Base {
};
Topic archived. No new replies allowed.