I am just starting into C++ and created a binary search tree. Now I want to pretty print this tree using operator overloading for cout.
//declared in BST class
void printTree(Node* n, int indent) const;
friend ostream& operator<<(ostream& os, const BinarySearchTree& b);
void BinarySearchTree::printTree(Node * n, int indent) const
{
if (n != nullptr)
{
printTree(n->left, indent + 4);
if (indent > 0)
cout << setw(indent) << " ";
cout << n->data << endl;
printTree(n->right, indent + 4);
}
return;
}
ostream & operator<<(ostream & os, const BinarySearchTree & b)
{
b.printTree(b.root, 4);
}
In the b.printTree(b.root, 4)
I get following errors
- initial value of reference to non-const must be an lvalue
- 'return': cannot convert from 'void' to 'std::ostream &'
Can anyone help me as to how to resolve these issue and what's the best way of going about pretty printing a BST by operator overloading in case my way is incorrect/not optimal
Aucun commentaire:
Enregistrer un commentaire