954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Overload Operator<< for a Binary Search Tree. Getting C.E. C3861

Hey Everyone,

So, I'm having an issue with a compiler error (C3861 to be exact). I'm creating a Binary Search Tree for my C++ course. The Binary Search Tree needs to overload the << operator & print the data in the tree inorder. My initial thought on solving this problem was to simply overload << as usual, & call a private method which would return the output to <<. However, this has lead to my compiler error. Any help would be appreciated!

My .h file:

class BSTree {

friend ostream& operator<<(ostream&, const BSTree&);

public:
	BSTree();
	BSTree(const BSTree &);
	~BSTree();
...
private:
ostream& outputHelper(ostream&, Node *);
...


My .cpp file:

ostream& BSTree::outputHelper(ostream &output, Node *root) {
	if(root != NULL) {
		outputHelper(output, root->left);
		output << root->data;
		outputHelper(output, root->right);
	}
	return output;
}

ostream& operator<<(ostream &output, const BSTree &other) {
	output << outputHelper(&output, other.overallRoot);
	return output;
}
RingmasterTJ
Newbie Poster
11 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

In your helper function:

ostream& operator<<(ostream &output, const BSTree &other) {
	output << outputHelper(&output, other.overallRoot);
	return output;
}


It doesn't know what outputHelper is supposed to be.

You could attempt to call it with other.outputHelper(output, other.overallRoot); but outputHelper is private (as I suspect overallRoot is).

I would tend to provide a public method, something like DumpInorder(ostream & output) that would make the call to outputHelper(output, overallRoot). Then your helper function looks more like:

ostream& operator<<(ostream &output, const BSTree &other) {
	other.DumpInorder(output);
	return output;
}
Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

Sigh, I knew it would be something simple...Thank you, you've made my life a lot easier today!

-TJ

RingmasterTJ
Newbie Poster
11 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You