首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法从模板成员函数调用模板成员函数

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

Stack Overflow用户
提问于 2012-08-28 17:25:53
回答 1查看 112关注 0票数 0

我成功地实现了一个集合模板作为AVL平衡二进制搜索树。现在,我正在努力使代码更短,更具可读性。在使用left_or_right将fix_imbalance_left和fix_imbalance_right合并到fix_imbalance模板中时,我遇到了问题。我一步一步地重新开始,现在我在fix_imbalance(left_or_right,node),得到以下错误:

代码语言:javascript
运行
复制
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后面的两个模板参数,但这些都没有帮助。

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

更多代码:

代码语言:javascript
运行
复制
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)
{ ... }

抱歉,我没有早点发布这篇文章。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-28 18:39:17

我认为您正在尝试从一个变量实例化一个模板。相反,尝试:

代码语言:javascript
运行
复制
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);
    ...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12156334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档