我收到了这个错误:
prog.cpp: In instantiation of 'class Tree<int, C<int> >':
prog.cpp:13:16: error: invalid covariant return type for
'Self* Tree<LeafT, Self>::branch() [with LeafT = int; Self = C<int>]'
Self * branch() {}
^
prog.cpp:7:34: error: overriding
'ATree<LeafT>* ATree<LeafT>::branch() [with LeafT = int]'
virtual ATree< LeafT> * branch() = 0;
^
在尝试针对此代码实例化C< int>时:
template< typename LeafT>
class ATree {
public:
virtual ATree< LeafT> * branch() = 0;
};
template< typename LeafT, typename Self>
class Tree : public ATree< LeafT> {
public:
Self * branch() {}
};
template< typename LeafT>
class C : public Tree< LeafT, C< LeafT> > {};
这是我的真实代码(http://ideone.com/lCw5OT)的模型,我不明白为什么我会得到协变返回错误,因为C< int>实际上是Tree< int, C< int>的孩子>这是ATree< int>的一个孩子!
我正在使用 C++98,可悲的是,不能使用 C++11 扩展(如果有帮助的话)。
我错过了什么?