I try to create a generic binary tree class but when i try to define an inserting function I get these compilation errors

error C2072: 'Btree<T>::recins' : initialization of a function
error C2143: syntax error : missing ';' before '*' c:\documents and settings\kux\my documents\visual studio 2005\projects\btrees\btrees\btree.h 54
Error 6 error C2275: 'Btree<T>::Element' : illegal use of this type as an expression c:\documents and settings\kux\my documents\visual studio 2005\projects\btrees\btrees\btree.h 54
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\kux\my documents\visual studio 2005\projects\btrees\btrees\btree.h 54
Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\kux\my documents\visual studio 2005\projects\btrees\btrees\btree.h 54
Error 9 fatal error C1903: unable to recover from previous error(s); stopping compilation c:\documents and settings\kux\my documents\visual studio 2005\projects\btrees\btrees\btree.h 54

Recommended Answers

All 6 Replies

I try to create a generic binary tree class but when i try to define an inserting function I get these compilation errors

error C2072: 'Btree<T>::recins' : initialization of a function
error C2143: syntax error : missing ';' before '*'
error C2275: 'Btree<T>::Element' : illegal use of this type as an expression
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
fatal error C1903: unable to recover from previous error(s); stopping compilation

this is the code:

template <class T>
class Btree
{
private:
	
	struct Element
	{
		T data_;
		Element* left_;
		Element* right_;
		Element( T &obj, Element* left = NULL, Element* right = NULL ):data_(obj), left_(left), 
			right_(right) {};
	};
	Element* root_;

	Element* recins( T& obj, Element* cpoz );
	void recprint( Element* cpoz );

public:
	Btree( T& obj );
	Btree( const Btree& btree );
	Btree& operator=(const Btree &btree);

	void insert( T& obj  );
	void printAll();

};


template <class T>
Btree<T>::Btree( T& obj)
{
	root_ = new Element( obj );
};


template <class T>
void Btree<T>::insert( T& obj )
{
	if( root_->data_ < obj )
		root_->left_ = recins( obj, root_->left_ );
	else 
		root_->right_ = recins( obj, root_->right_ );
};



template <class T>
Element* Btree<T>::recins( T& obj, Element* cpoz )
{
	if ( cpoz == NULL )
	{
		Element *p = new Element( T );
		return p;
	}
	else if ( cpoz->data_ < obj )
	{
		cpoz = recins( obj, cpoz->left_ );
	}
	else
	{
		cpoz = recins( obj, cpoz->right_ );
	}
};

any advice would be helpfull
thx in advance :)

Shouldnt you show your code so people could look for the problem?

>template <class T>
>Element* Btree<T>::recins( T& obj, Element* cpoz )
Element is a dependent type, so you need to be very specific about what it is:

template <class T>
typename Btree<T>::Element* Btree<T>::recins( T& obj, Element* cpoz )

>Element *p = new Element( T );
T is a type, you probably meant to use the default value for T like this:

Element *p = new Element( T() );

Also, recins needs to return a value for all execution paths, and you need to include a header (I recommend <cstddef>) that defines the NULL macro.

scratch this

thx a lot dude, the typename Btree<T>::Element* was the answer :), the Element *p = new Element( T ) was not what I meant to write, but I overlooked it when sent the mail. cheers

Forget NULL macros once and for all. Use pointer constant 0.
Wait for nullptr reserved word in future standard releases;)..

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.