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;
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.