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;
}

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;
}

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

-TJ

This article has been dead for over six months. Start a new discussion instead.