0

My printTree function won't work in the overloaded ostream operator. Error and code below.

Error code : C3861 ('printTree': identifier was not found)

Description: 'printTree': function was not declared in the template definition context and can be found only via argument-dependent lookup in the instantiation context

Class Declaration

#pragma once
#include "BSTNode.h"
#include <iostream>
template <typename T> class BST;
template <typename T>
std::ostream& operator<<(std::ostream& out, const BST<T>& rhs);

template <typename ItemType>
class BST
{
private: 
    BSTNode<ItemType>* root;
    BSTNode<ItemType>* insert(ItemType* &data, BSTNode<ItemType>* root);
    BSTNode<ItemType>* remove(const ItemType &x, BSTNode<ItemType>* root);
    BSTNode<ItemType>* findMin(BSTNode<ItemType>* root) const;
    BSTNode<ItemType>* findMax(BSTNode<ItemType>* root) const;
    bool search(const ItemType &data, BSTNode<ItemType>* root) const;
    void destroyTree(BSTNode<ItemType>* root); //for destructor
    BSTNode<ItemType>* clone(BSTNode<ItemType>* root) const;
    void printTree(std::ostream& out, BSTNode<ItemType>* root) const;

public:  
    BST();
    ~BST();
    BST(const BST &rhs); 
    BSTNode<ItemType>* getRoot() const {return root;}
    bool isEmpty(); 
    void insert(ItemType* &data);
    bool remove(const ItemType &x);
    ItemType findMin() const; 
    ItemType findMax() const;
    bool search(const ItemType &data) const;
    friend std::ostream& operator<< <>(std::ostream & out ,const BST<ItemType> &rhs);  
    void printTree (std::ostream &out = std::cout) const; 
};

Relevant Methods

template <typename ItemType>
void BST<ItemType>::printTree(std::ostream& out, BSTNode<ItemType>* root) const
{
   if (root != nullptr)
   {
      printTree(out, root->getLeft());
      out << *(root->getData()) << std::endl;
      printTree(out, root->getRight());
   }
}


template <typename ItemType>
std::ostream& operator << <>(std::ostream & out , const BST<ItemType> &rhs)
{
   printTree(out, rhs.getRoot());
   return out; 
}

Solved:

Needed to add calling object to printTree function in operator overload definition

template <typename ItemType>
std::ostream& operator << <>(std::ostream & out , const BST<ItemType> &rhs)
{
   rhs.printTree(out, rhs.getRoot());
   return out; 
}
Teoh Ryan
  • 21
  • 1
  • 1
    You may want to expand on why those methods are relevant. It cannot easily be inferred from the question. When providing an error message, it is important to provide the full text of the error message. The Output tab, found not far from the Error List in Visual Studio, provides the full error message and often the full message provides additional hints to solving the problem, like where in the code it is, that the error code and its explanation from the compiler documentation, does not. – user4581301 Feb 29 '20 at 05:27
  • 1
    In this case, `printTree` is a `BST` member function and the `<<` overload is calling it without an instance (eg: `rhs.printTree(out, rhs.getRoot());`). This may be the error you are looking for, but it could also be an unrelated error. It is hard to be certain. – user4581301 Feb 29 '20 at 05:31
  • Can't believe I missed that. Thanks a lot. I agree with your input, I'll add more details in case someone else stumbles upon this thread – Teoh Ryan Feb 29 '20 at 05:43

1 Answers1

0
std::ostream& operator<<(std::ostream& out, const BST<T>& rhs);
friend std::ostream& operator<< <>(std::ostream & out ,const BST<ItemType> &rhs);

Have <> as a difference so I'm not sure if this is your error. Same with the method.

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32