C++ 尝试调用模板类的友元函数
我有一个模板,其中有一个friend函数的声明,在类之外,我有它的实现:C++ 尝试调用模板类的友元函数,c++,templates,friend,C++,Templates,Friend,我有一个模板,其中有一个friend函数的声明,在类之外,我有它的实现: 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); ... } temp
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{
公众:
模板
friend AVLTree createEmptyAVLTree(int n);
...
}
模板
AVLTree createEmptyAVLTree(int n){…}
在其他文件中的某个地方调用它的签名是什么
我试过:
AVLTree<Post, postByLikesFunc, emptyFunc>::createEmptyTree();
AVLTree::createEmptyTree();
但它说它无法解决这个问题。为什么?朋友成员应该被这样看待,不是吗
编辑:
AVLTree<Post, postByLikesFunc, emptyFunc> empty;
empty = createEmptyTreeAVLTree<Post, postByLikesFunc, emptyFunc>(size);
empty.arrayToTree(sorted_posts);
AVLTree空;
empty=createEmptyTreeAVLTree(大小);
空.arrayToTree(已排序的_帖子);
这在Troll.cpp的函数中
仍然会喊出“未在此范围内声明”、“函数无法解析”、“符号无法解析”、“之前应为主要表达式”、“之前应为主要表达式>”#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
\ifndef AVL\u水电站
#定义AVL_水电站
模板
类AVLTree{
公众:
模板
friend AVLTree createEmptyAVLTree(int n);
};
模板
AVLTree createEmptyAVLTree(int n){
返回AVLTree();
}
#恩迪夫
main.cpp
#include "AVL.hpp"
int main() {
createEmptyAVLTree<int, int, int>(4);
return 0;
}
#包括“AVL.hpp”
int main(){
createEmptyAVLTree(4);
返回0;
}
createEmptyAVLTree不在AVLTree的范围内。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
\ifndef AVL\u水电站
#定义AVL_水电站
模板
类AVLTree{
公众:
模板
friend AVLTree createEmptyAVLTree(int n);
};
模板
AVLTree createEmptyAVLTree(int n){
返回AVLTree();
}
#恩迪夫
main.cpp
#include "AVL.hpp"
int main() {
createEmptyAVLTree<int, int, int>(4);
return 0;
}
#包括“AVL.hpp”
int main(){
createEmptyAVLTree(4);
返回0;
}
createEmptyAVLTree不在AVLTree的范围内。ok,如果main在其他文件中?这就是我现在所做的,在另一个文件中它仍然“无法解决”@KittyT2016它也可以工作。你能给我看更多的代码吗?@KittyT2016,它是createEmptyAVLTree而不是createEmptyTree,我在它的函数中的另一个对象中使用它。更新了错误列表,仍然是几乎相同的问题。确定,如果主文件在其他文件中?这就是我现在所做的,在另一个文件中它仍然“无法解决”@KittyT2016它也可以工作。你能给我看更多的代码吗?@KittyT2016,它是createEmptyAVLTree而不是createEmptyTree,我在它的函数中的另一个对象中使用它。更新了错误列表,仍然是几乎相同的问题。解决了它。就像斯拉尔达说的,解决了。就像斯拉尔达说的。