I'm working on a Binary Search Tree class, and am having trouble coding an overloaded streaming operator function. Here's my code...I've done everything I've managed to find online (and in my professor's powerpoints), so I have no idea what I'm doing wrong.
*edited a few seconds ago to update changes to my code. Decided to call function from object being sent into friend that has similar code to that which was in the friend function...
Relevant headers in header file (.h file):
friend ostream& operator<<(ostream& out, const BST& tree);
void leveltraversal(ostream& out);
Relevant "private" data / nested classes in header file (.h file):
private:
class BinNode {
public:
T data;
BinNode* left;
BinNode* right;
BinNode() : left(NULL), right(NULL) {}
BinNode (T item): data(item), left(NULL), right(NULL) {}
};
typedef BinNode* BinNodePtr;
BinNodePtr myRoot;
Relevant implementation file functions:
ostream& operator<<(ostream& out, const BST& tree)
{
tree.leveltraversal(out);
return out;
}
template <typename T>
void BST<T>::leveltraversal(ostream& out)
{
int level = 0;
BinNodePtr temp = myRoot;
queue<BinNodePtr> nodes;
nodes.push(temp);
out << endl << endl;
while (!nodes.empty()){
temp = nodes.front();
level = recursive_level(temp->data);
out << endl << endl;
out << "Node data: " << temp->data;
out << endl;
out << "Level: " << level;
nodes.pop();
if (temp->left)
nodes.push(temp->left);
if (temp->right)
nodes.push(temp->right);
}
}
I'd post the compiler errors, but they go on for quite a few lines and I feel that the problems are self evident. Will update with them, though, if anyone would like!