我收到了这个错误:
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&lt;对此代码进行int&gt; :
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&lt; int&gt; 实际上是 Tree&lt; int,C&lt; INT&GT; &gt; 是 ATree的孩子&lt; INT&GT; <!/ em>的
我使用的是C ++ 98,遗憾的是,它不能使用C ++ 11扩展(如果可能有帮助的话)。
我失踪了什么?