Can't get my program compile. For historical reason I have a bit "complicated" hierarchy of the classes in my application. And now I faced with another problem: it won't compile because of this hierarchy. My project is too big to show it here, but below you can see the example that demonstrates the problem. Is there is a simple and elegant way to fix it? Every interface/class has really big amount of methods. Thanks in advance.
struct ITest
{
virtual ~ITest() {}
virtual void a() = 0;
// and many other methods
};
struct Test1 : public ITest
{
virtual ~Test1() {}
virtual void a() override {}
// and many other methods overrides from ITest
// plus a lot of other logic
};
struct ExtendedTest1 : public Test1
{
virtual ~ExtendedTest1() {}
// a lot of some other stuff
};
struct ITest2 : public ITest
{
virtual ~ITest2(){}
// and big count of the its methods and logic
};
struct MainClass : public ExtendedTest1, public ITest2
{
virtual ~MainClass(){}
// a lot of logic
};
int main()
{
MainClass mainClassObj;
return 0;
}
And the errors:
main.cpp: In function ‘int main()’:
main.cpp:36:15: error: cannot declare variable ‘mainClassObj’ to be of abstract type ‘MainClass’
MainClass mainClassObj;
^~~~~~~~~~~~
main.cpp:28:8: note: because the following virtual functions are pure within ‘MainClass’:
struct MainClass : public ExtendedTest1, public ITest2
^~~~~~~~~
main.cpp:4:18: note: virtual void ITest::a()
virtual void a() = 0;
^
Do not judge strictly :)
UPD: before asking this question I really tried virtual inheritance to solve my problem but it didn't work. So after suggestion to try it again, it works) So, replacing these lines solved my problem:
struct Test1 : public ITest ---> struct Test1 : virtual public ITest
struct ITest2 : public ITest ---> struct ITest2 : virtual public ITest
I know, we must avoid virtual inheritance, but we can't 'cause of historical reason and very big amount of code
Thanks everybody for help!
You need Virtual Inheritance.
Virtual inheritance can get rid of redundant functions:
Instead, if classes B and C inherit virtually from class A, then objects of class D will contain only one set of the member variables from class A.
If you inherit virtually, you will get rid of your duplicate abstract function.