我成功地将Set模板实现为AVL平衡二叉搜索树。现在我正在尝试使代码更短,更易读。将fix_imbalance_left和fix_imbalance_right合并到使用left_or_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]
请注意,旋转(节点)模板已实现并合并(左侧+右侧插入模板),并且在使用单独的fix_imbalance之前已成功完成。 我已经尝试过:'this-&gt;'并在单独的&lt;&gt;中指定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)
{ ... }
抱歉,我之前没有发布此内容。
答案 0 :(得分:0)
我认为您正在尝试从变量中实例化模板。请尝试改为:
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);
...
}