[英]Member function inside templated operator overload not working
My printTree function won't work in the overloaded ostream operator.我的 printTree 函数在重载的 ostream 运算符中不起作用。 Error and code below.错误和代码如下。
Error code : C3861 ('printTree': identifier was not found)错误代码:C3861('printTree':未找到标识符)
Description: 'printTree': function was not declared in the template definition context and can be found only via argument-dependent lookup in the instantiation context说明:“printTree”:函数未在模板定义上下文中声明,只能通过实例化上下文中的参数相关查找找到
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需要在运算符重载定义中将调用对象添加到 printTree 函数
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.方法同上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.