无法从模板成员函数调用模板memeber函数

时间:2021-09-04 23:13:07

I successfully implemented a Set template as an AVL balanced binary search tree. Now I am trying to make the code shorter and more readable. When merging fix_imbalance_left and fix_imbalance_right into fix_imbalance templated with left_or_right, I ran into problems. I started over step by step, and now I am at fix_imbalance(left_or_right,node) and get the following error:

我成功地将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]

Note that the rotate(node) template is implemented and merged (left+right into template) and was successfull before with the separate fix_imbalance's. I already tried: 'this->' and specifying both template arguents after fucntionname in separate <>'s, but none of those helped.

请注意,旋转(节点)模板已实现并合并(左侧+右侧进入模板),并且在使用单独的fix_imbalance之前已成功完成。我已经尝试过:'this->'并在单独的<>中指定fucntionname之后的两个模板争论者,但这些都没有帮助。

Could you please point out what am I doing wrong ?

你能指出我做错了什么吗?

more code:

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)
{ ... }

Sorry I did not post this earlier.

对不起,我之前没有发布。

1 个解决方案

#1


0  

I think you are trying to instantiate a template from a variable. Try instead:

我想你正在尝试从变量中实例化一个模板。尝试改为:

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);
    ...
}

#1


0  

I think you are trying to instantiate a template from a variable. Try instead:

我想你正在尝试从变量中实例化一个模板。尝试改为:

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);
    ...
}