Say I have 3 classes with following topology, and the actual code is somehow like this:
// formal system.h
#pragma once
namespace nianyi {
class Proposition {
public:
class Term;
class Expression;
// Term::Value evaluate();
};
class Proposition::Term : public Proposition {
public:
class Variable;
class Value;
};
class Proposition::Term::Value : public Proposition::Term {
};
};
It's easy to see that if I uncomment that function declaration, the compiler is gonna complain about the un-previously-defined class Value, and indeed it does:
error C2027: use of undefined type 'nianyi::Proposition::Term'
But on the other hand, class Term
is derived from its base class Proposition
, which means I can't put its implementation in the base class since it's not a complete class before the implementation ends, and an incomplete class cannot be derived from.
Thus, putting the implementation of class Term
both inside & outside class Proposition
would lead to a compiler error. The inside one is caused by the incompleteness of the base class, and the outside one is caused by the incompleteness of the derivative.
So... what's the best way to solve this?
P.S. please stop telling "you should complete the definition before using a class" or "just don't make them nested". For the first kinda words, that's the whole problem there, simply telling this doesn't really help much; and for the second kinda words, I'm just intended to make them nested to not to expose those details.