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?

Recommended Answers

All 2 Replies

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.

I see what I did wrong. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.