我成功地实现了一个集合模板作为AVL平衡二进制搜索树。现在,我正在努力使代码更短,更具可读性。在使用left_or_right将fix_imbalance_left和fix_imbalance_right合并到fix_imbalance模板中时,我遇到了问题。我一步一步地重新开始,现在我在fix_imbalance(left_or_right,node),得到以下错误:
adts/implementations/set.cpp:224:3: error: no matching function for call to ‘Set<int>::rotate(Set<int>::nodeT*&)’
adts/implementations/set.cpp:224:3: note: candidate is:
adts/implementations/set.cpp:70:39: note: template<Set<int>::directionT DIRECTION> void Set::rotate(Set<ElemT>::nodeT*&) [with Set<ElemT>::directionT DIRECTION = L, ElemT = int]注意,rotate(节点)模板已经实现并合并(合并到模板中),并且之前已经成功地使用了独立的fix_left+right‘s。我已经尝试了:'this->’,并在单独的<>中指定了fucntionname后面的两个模板参数,但这些都没有帮助。
你能指出我做错了什么吗?
更多代码:
enum directionT { LEFT=0, RIGHT=1 }; // inside class definition
template <directionT DIRECTION> void rotate(nodeT * & t); // line 70, inside class def
template <typename ElemT>
bool Set<ElemT>::fix_imbalance(directionT direction, nodeT * & node)
{
directionT R = (direction==LEFT) ? RIGHT : LEFT;
...
rotate<R>(node); // line 224
...
}
// this below worked before,
// when fix_imbalance_left and fix_imbalance_right were separate
// there I called rotate<LEFT>(node); and rotate<RIGHT>(node); and it worked
template <typename ElemT>
template <typename Set<ElemT>::directionT L>
void Set<ElemT>::rotate(nodeT * & t)
{ ... }抱歉,我没有早点发布这篇文章。
发布于 2012-08-28 18:39:17
我认为您正在尝试从一个变量实例化一个模板。相反,尝试:
template <typename ElemT>
bool Set<ElemT>::fix_imbalance(directionT direction, nodeT * & node)
{
directionT R = (direction==LEFT) ? RIGHT : LEFT;
...
if (R==LEFT)
rotate<LEFT>(node); // line 224
else
rotate<RIGHT>(node);
...
}https://stackoverflow.com/questions/12156334
复制相似问题