0

I'm having trouble with a multiple inheritance usage case (it's not my project, I didn't decide the inheritance scheme, sorry). Anyway, I can't seem to call a protected virtual member function from an overriding function. Here's a simplified class structure:

class Base {
    protected:
        virtual void foo() { }
};

class A: virtual public Base {
    protected:
        virtual void foo();
};
void A::foo() { printf("Hello A\n"); }

class B: virtual public Base {
    protected:
        virtual void foo();
};
void B::foo() { printf("Hello B\n"); }

class AB: public A, public B {
    protected:
        void foo();
};
void AB::foo()
{
    A::foo();
    B::foo();
}

I would expect that if I call AB's foo(), I'll get output that looks like

Hello A
Hello B

However, when I compile, I get an errors like:

error #308: function "A::foo" is inaccessible
    A::foo();
       ^
error: a nonstatic member reference must be relative to a specific object
    A::foo();
    ^

error #308: function "B::foo" is inaccessible
    B::foo();
       ^
error: a nonstatic member reference must be relative to a specific object
    B::foo();
    ^

Even if I make all of the foos public, I still see this. What's wrong?

4
  • 2
    Works in MSVC. What compiler are you using? It looks like it thinks AB::foo() is not a member of AB, double check the signature is identical in your non-simplified version..
    – VoidStar
    Sep 30, 2015 at 18:38
  • Yes, compiles fine in MSVC! Please state the compiler, version, and any compiler flags (if you are using any).
    – zeropoint
    Sep 30, 2015 at 18:42
  • codepad.org/7vxKWShz
    – DrWatson
    Sep 30, 2015 at 18:43
  • I realized that I had typo'd -- void foo() and void AB::foo() are very different!
    – kc9jud
    Sep 30, 2015 at 18:47

2 Answers 2

0

The specific object can be *this, so this->A::foo() and this->B::foo().

0

I was not defining my function in the correct namespace. void foo() and void AB::foo() are very different!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.