I came across a problem while trying to reuse code from different classes. I am posting it here in the hope that some of you can help me.
I have a set of classes (B, C) derived from the same class (A), which forces some methods to be implemented (foo, run). Class B implements this method, and both B and C provide other methods:
#include<iostream>
template<class I, class O>
class A {
public:
A() {}
virtual ~A() {}
virtual void foo() const = 0;
virtual void run() const = 0;
};
template<class I, class O>
class B : public A<I,O> {
public:
B() {}
virtual ~B() {}
virtual void foo() const {
std::cout << "B implementation of foo" << std::endl;
}
virtual void run() const {
std::cout << "B implementation of run" << std::endl;
}
virtual void foobar() const {
std::cout << "B implementation of foobar" << std::endl;
}
};
template<class I, class O, class M>
class C : public A<I,O> {
public:
C() {}
virtual ~C() {}
virtual void bar(M m) const {
std::cout << "C implementation of bar with: " << m << std::endl;
}
};
Now, what I'm trying to do is inherited from both B and C, so I can have additional methods (foobar, bar), but I also don’t need to implement a method from class A (foo), because it is already defined in B:
template<class I, class O>
class D : public B<I,O>, public C<I,O,int> {
public:
D() {}
void run() const {
this->bar(123);
this->foo();
this->foobar();
}
};
But for some reason, the compiler gives me this error:
test.cpp: 'int main (int, char **): test.cpp: 68: 35: : 'D < float, double >
A < float, double > * d = D < float, double > ();//
test.cpp: 48: 11: note: 'D < float, double > :
D: public B < I, O > , public C < I, O, int > {
^
test.cpp: 9: 22: note: void A < I, O > :: foo() const [ = float; O = double]
virtual void foo() const = 0;//
, :
int main(int argc, char **argv)
{
A<float, double> *b = new B<float, double>();
b->foo();
b->run();
return 0;
}
"run" D A. , , , , "B:: run ' . " D:: run" B C, .
, .
!