【问题标题】:Trying to call a friend function of template class试图调用模板类的友元函数
【发布时间】:2016-04-27 10:23:03
【问题描述】:

我有一个模板,其中我有一个友元函数的声明,在类之外,我有它的实现:

template<class TreeElement, class Comparator, class Operation>
class AVLTree {
public:
     template<class A, class B, class C >
     friend AVLTree<A, B, C> createEmptyAVLTree(int n);
...
}
template<class A, class B, class C>
AVLTree<A, B, C> createEmptyAVLTree(int n) { ... }

在其他文件的某处调用它的签名是什么?

我试过了:

AVLTree<Post, postByLikesFunc, emptyFunc>::createEmptyTree();

但它说它无法解决它。为什么 ?朋友成员应该是这样看的吧?

编辑:

AVLTree<Post, postByLikesFunc, emptyFunc> empty;
empty = createEmptyTreeAVLTree<Post, postByLikesFunc, emptyFunc>(size);
empty.arrayToTree(sorted_posts);

它在 Troll.cpp 的功能中。

仍然喊“未在此范围内声明”、“函数无法解析”、“符号无法解析”、“预期的主表达式之前”、“预期的主表达式之前>”

【问题讨论】:

  • 解决了。就像斯拉达说的那样。

标签: c++ templates friend


【解决方案1】:

AVL.hpp

#ifndef AVL_hpp
#define AVL_hpp

template<class T1, class T2, class T3>
class AVLTree {
public:

    template<class A, class B, class C>
    friend AVLTree<A, B, C> createEmptyAVLTree(int n);
};

template<class A, class B, class C>
AVLTree<A, B, C> createEmptyAVLTree(int n) {
    return AVLTree<A,B,C>();
}

#endif

main.cpp

#include "AVL.hpp"

int main() {
    createEmptyAVLTree<int, int, int>(4);
    return 0;
}

createEmptyAVLTree 不在 AVLTree 的范围内。

【讨论】:

  • 好的,如果 main 在其他文件中?这就是我现在所做的,在其他文件中它仍然“无法解决”
  • @KittyT2016 它也可以。你能告诉我更多代码吗?
  • @KittyT2016 它是 createEmptyAVLTree 而不是 createEmptyTree
  • 我在另一个对象中使用它的功能。更新了错误列表,还是几乎一样的问题。
猜你喜欢
  • 2010-12-19
  • 2011-07-15
  • 1970-01-01
  • 2016-10-19
  • 2013-09-18
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多