0

I am gettin this compile error and for the life of me cannot figure it out:

BinarySearchTree.h:22: error: type âintâ is not a direct base of âBinarySearchTree<int, int, std::less<int> >::BinaryNodeâ

This is the header file file the error refers:

#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include <cstdlib>
#include <functional>

template <class T, class Key = T, class Compare = std::less<Key> >
class BinarySearchTree
{
	public:
		BinarySearchTree();
		void InsertSorted(const T& data);
		
		void InsertSorted(const Key& key, const T& data);
	
	private:
		struct BinaryNode
		{
			T data;
			Key key;
			BinaryNode *left;
			BinaryNode *right;
			BinaryNode(Key k, T i) : data(i), Key(k) {left = NULL; right = NULL;}
		};
		BinaryNode *root;
		
		Compare compare;
		
		void InsertSortedR(const Key& key, const T& data, BinaryNode*& node);
};

#include "BinarySearchTree.cpp"

#endif

Do I have too many assignments in the struct constructor?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by DLightman
1

Ok the problem, that doesn't actually occur until you try to create a version of the BinaryNode (from BinarySearchTree) is that you have written:

BinaryNode(Key k, T i) : data(i), Key(k)

Note that Key is now a type [From Line 6].

You need to use key. Or please use a different word. (I like "key" for
your key name that is fine but the word "Key" as the template typename does not convey the sense of type)

Additionally, please use the references uniformly, for example if you have a key (say a string) by not having BinaryNode take a reference,
there is additional coping. Although, some compilers will help you on the optimization pass.

Hope this helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.