I have a superclass with a class defined inside it. like:
class A {
public:
class B {public: bool value;};
A() {
DoStuff(b_);
}
B b_;
private:
virtual void DoStuffImpl(B& b) = 0;
void DoStuff(B& b) { return DoStuffImpl(b); }
};
class X : public A {
// ...
private:
virtual void DoStuffImpl(B& b);
void UseBForSomethingElse(B& b);
};
void X::DoStuffImpl(B& b) {
UseBForSomethingElse(b);
}
void X::UseBForSomethingElse(B& b) {
b.value = true;
}
int main(){
X x;
return x.b_.value;
}
My compiler seems to understand that DoStuffImpl()
just fine. But, when I added UseBForSomethingElse()
, the compiler could not find the definition for the B
class. I tried to further specify by doing bool UseBForSomethingElse(A::B& b)
. This compiled, but then failed during linking.
How do I correctly specify the parent B
, and why does it work for the virtual function but not the other one?
Your updated post doesn't properly qualify UseBForSomethingElse().
void UseBForSomethingElse(const B& b)
should be
void X::UseBForSomethingElse(const B& b)
Once this is fixed, you still have a problem (and heaven help me if I get this wrong).
You're firing a virtual method from a base class constructor without the derived class finishing construction. I.e, UseBForSomethingElse (non-virtual) is fired from DoStuffImpl() (virtual, pure @ A, defined in X) before X is finished base-construction (you're, in fact in X's base-construction when you make the call). This will trigger a 'pure virtual function called' since X's vtable isn't fixed up until its constructor is actually entered.
This does happen on my machine as well, btw.