2

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!

  • 3
    you might want to include the compiler errors/warnings – gvd Nov 05 '12 at 07:08
  • Oh yeah! Sorry...I've been coding all day, and am a little tired. Thanks for helping :) – user1799411 Nov 05 '12 at 07:29
  • It's hard to say without the error messages (the first one (or few) is often sufficient) but first of all: what are you instantiating the `BST` template with, i.e. what is the type of `temp->data`? – molbdnilo Nov 05 '12 at 16:27

1 Answers1

0

Since you haven't listed the error messages or even stated the type of problem you're having, it's difficult to help. However I tried to fill in some blanks to replicate your issue and found a couple of problems with the code:

template <typename T>
class BST {
   ...
   friend std::ostream& operator<<(std::ostream& out, const BST& tree)
   {
       tree.leveltraversal(out);
       return out;
   }
  • In operator<< you take a const BST& tree (meaning you can't change the original object though this reference), therefore the leveltraversal function must also be declared const. You cannot call a non-const member function on a const object; if allowed that would let you modify the object, breaking the constness.
void leveltraversal(std::ostream& out) const;

With these changes, the code built fine for me with both clang and g++.

Community
  • 1
  • 1
Anders Johansson
  • 3,926
  • 19
  • 19