0

The following code works perfectly fine:

#include "stdafx.h"

class A1
{
public:
    virtual void a1() = 0;
};

class B1 : virtual public A1
{
public:
    virtual void b1()
    {
        A1::a1();
    }
};

class A1_Impl : virtual public A1
{
public:
    virtual void a1() {}
};

class B_Combined : virtual public A1_Impl/*, virtual public B1*/
{
};

int _tmain(int argc, _TCHAR* argv[])
{
    B_Combined b;
    return 0;
}

After removing /* and */ the compiler presents the "'B_Combined': inherits 'A1_Impl::A1_Impl::a1' via dominance" warning. I guess thats okay, because I want B_Combined to inherit the implementation of A1::a1 in A1_Impl (its the only implementation).

But I also get an error message:

error LNK2001: unresolved external symbol ""public: virtual void __thiscall A1::a1(void)" (?a1@A1@@UAEXXZ)".

I am a bit confused about this. Why doesn't the implementation in A1_Impl work? Deriving B_Combined from B1 should only add a new method b1 calling the implementation of A1::a1 in A1_Impl.

1 Answers1

0

The code you posted compiles fine. However The linkage error that you mentioned will result from declaring

class B_Combined : virtual public A1_Impl, virtual public B1
{
};

for you do call A1::a1(); in virtual void B1::b1, but A1::a1 is pure abstract. You must not call A1::a1 unless you provide a body for it.

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Yes you are right, you have to remove the comment to see the problem. But I still do not get why the unresolved symbol is there. As you said I call A1::a1() which is not implemented in B1 but it is implemented in A1_Impl, doesn't this count? – user3369521 Mar 03 '14 at 21:19
  • no, classes B1 abd A1_impl doesn't share implementation. They both derive from A1 and they both has to implement virtual a1 if it is to be used. Moreover you can never call A1::a1() directly, because this is static call and function has no body – 4pie0 Mar 03 '14 at 21:33
  • @user3369521 do you have additional questions? – 4pie0 Mar 04 '14 at 02:09