I have the following structure:
Two possible algorithms, one inherits the other, whose template parameter is a self defined container class.
template <class C>
class AlgBase {
protected:
C c;
};
template <class C>
class AlgDerived : public AlgBase<C> {};
The container class is templated as well, and the parameters are also related by inheritance:
class TParamBase {};
class TParamDerived : public TParamBase {};
template <class T> // T is TParamBase or TParamDerived
class Container {
protected:
T t_;
};
Now, in my main, I want to do the following:
typedef Container<TParamBase> ContainerBase;
typedef Container<TParamDerived> ContainerDerived;
int main (int argc, const char ** argv)
{
vector<AlgBase<ContainerBase>* > algs;
algs.push_back(new AlgBase<ContainerBase>);
algs.push_back(new AlgDerived<ContainerBase>);
algs.push_back(new AlgDerived<ContainerDerived>);
}
The first two push_back
work since they use ContainerBase
. The third fails because the use of ContainerDerived
. The error is:
error: no matching function for call to ‘std::vector<AlgBase<Container<TParamBase> >*>::push_back(AlgDerived<Container<TParamDerived> >*)’
algs.push_back(new AlgDerived<ContainerDerived>);
^
main.cpp:34:52: note: candidates are:
In file included from /usr/include/c++/4.9/vector:64:0,
from main.cpp:3:
/usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = AlgBase<Container<TParamBase> >*; _Alloc = std::allocator<AlgBase<Container<TParamBase> >*>; std::vector<_Tp, _Alloc>::value_type = AlgBase<Container<TParamBase> >*]
push_back(const value_type& __x)
^
/usr/include/c++/4.9/bits/stl_vector.h:913:7: note: no known conversion for argument 1 from ‘AlgDerived<Container<TParamDerived> >*’ to ‘AlgBase<Container<TParamBase> >* const&’
/usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = AlgBase<Container<TParamBase> >*; _Alloc = std::allocator<AlgBase<Container<TParamBase> >*>; std::vector<_Tp, _Alloc>::value_type = AlgBase<Container<TParamBase> >*]
push_back(value_type&& __x)
^
/usr/include/c++/4.9/bits/stl_vector.h:931:7: note: no known conversion for argument 1 from ‘AlgDerived<Container<TParamDerived> >*’ to ‘AlgBase<Container<TParamBase> >*&&’
The only solution I have in mind is to forget about TParamBase
and TParamDerived
and only use one class, but this is a bit inefficient since I will be creating and allocating elements not used.
Is there any other solution to make the third push_back
work?
Thank you!
Aucun commentaire:
Enregistrer un commentaire