Home Compilation errors defining templated class friend funtions
Reply: 0

Compilation errors defining templated class friend funtions

cout
1#
cout Published in 2017-12-06 11:00:02Z

This question already has an answer here:

  • Where and why do I have to put the “template” and “typename” keywords? 5 answers
  • Officially, what is typename for? 7 answers
  • Why do we need typename here? 4 answers

Working on an assignment from my OOP class and I'm really overwhelmed with all the content I have to work with. I'm currently defining functions of a templated BST class and a nested Node class inside as well. I had to create a BST.cpp class since I'm using friend functions to overload operators so I couldn't put them in my BST.inl file.

Now I'm having problems with defining/declaring those functions and getting errors such as "'n1': undeclared identifier", "'Tree::BST::Node': dependent name is not a type", "unrecognizable template declaration/definition", and some others.

Could you help me fix the problem with my definitions/declarations?

BST.hpp

#ifndef BST_HPP
#define BST_HPP
#include <istream>

namespace Tree{
    template<class T>
    class BST {
    public:
        BST();//default constructor
        BST(T rootkey);//constructor with 1 parameter for root key
        ~BST();//destructor
        void insert(T value); //function named insert with 1 parameter(key value to insert)
        Node * remove(Node* troot, T value);//function named remove with 1 parameter (key value to remove)
        Node* min(Node * mini);
    private:
        class Node {
        public:
            Node();//default constructor
            Node(T k);//constructor with 1 parameter (key value)
            ~Node(); //destructor
            Node* getP(); //parent accessor
            Node* getL();//left accessor
            Node* getR();//right accessor
            T getK();//key accessor
            void setP(Node * prnt);//parent mutator
            void setL(Node * lft); //left mutator
            void setR(Node * rht); //right mutator
            friend bool operator <(const Node* n1, const Node* n2);
            friend bool operator ==(const Node* n1, const Node* n2);
            friend bool operator !=(const Node* n1, const Node* n2);
            //not needed operator <<;
        private:
            T key;
            Node *parent;
            Node *left;
            Node *right;
        };
        Node *root;
        void destroy(Node* r);


    };



};

#include "BST.inl"
#endif

BST.cpp

#include "BST.hpp"

template<class T>
bool Tree::operator<(const Tree::BST<T>::Node* n1, const Tree::BST<T>::Node* n2)//ERROR
{
    if (n1->getK() < n2->getK())
    {
        return true;
    }
    else
        return false;
}

template<class T>
bool Tree::operator==(const Tree::BST<T>::Node * n1, const Tree::BST<T>::Node * n2)//ERROR
{
    return false;
}

template<class T>
bool Tree::operator!=(const Tree::BST<T>::Node * n1, const Tree::BST<T>::Node * n2)//Error
{
    return false;
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305865 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO