Search code examples
c++templatesfriend

Trying to call a friend function of template class


I have a template, in which I have a declaration of a friend function, And lower, outside the class i have it's realization:

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

What is the signature to call it somewhere in other files ?

I've tried:

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

but it says it couldn't resolve it. Why ? Friend member should be seen just like that, aren't they ?

EDIT:

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

that's in Troll.cpp in it's function.

Still shouts "was not declared in this scope", " Function could not resolved", "Symbol couldnt not resolved", "expected primary-expression before ,", "expected primary-expression before >"


Solution

  • 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;
    }
    

    The createEmptyAVLTree isn't in the scope of AVLTree.