Recursive delete function errors

1
2
3
4
5
6
template<class _TYPE, class _VAL_TYPE>
bbst<_TYPE, _VAL_TYPE>::~bbst ()
{
	if (this->left != nullptr) delete *this->left;
	if (this->right != nullptr) delete *this->right;
}


I am attempting to use this function to dissasemble an entire binary search tree, however, i cant seem to access this->left which is a pointer to bbst<_TYPE, _VAL_TYPE>

it says:

error C2440: 'delete' : cannot convert from 'st::bbst<_TYPE,_VAL_TYPE>' to 'void *'
Last edited on
Don't dereference the pointer. You also don't need to check if the pointer is a null pointer because deleting a null pointer is safe and will do nothing.
1
2
delete this->left;
delete this->right;
i tried that, it still gave the same error, i found out that the destructor cant be recursive for some reason, when i did the same thing via a recursive function calling the destructor it worked, and im trying to delete the value of the pointer's address not the pointer itself
You should only delete what you have created with new.
@Peter: If you delete a null pointer nothing happens by design.
@GFreak: I hope you're not REALLY defining your functions like that...did you know that templated class functions have to be defined inline? Otherwise you have to explicitly instantiate the templates for the linker to fi8nd them.
@LB template functions are special and don't have to be inline.
@Peter i know, every single node besides the root are created dynamically, anyway i finished it and it works, not sure how i would submit it for public use though

The library i made was a balanced binary search tree capable of sorting classes or any type of object using a user defined function

ie:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <searchtree>

class a
{
public:
	a (int x) : value(x) {};
	
private:

	static int GetValue (a obj)
	{
		return obj->value;
	}

	static bbst<a, int> searchTree (GetValue);
	
	int value;
}
Last edited on
1
2
3
4
	static int GetValue (a obj)
	{
		return obj->value;
	}
That makes no sense.

static bbst<a, int> searchTree (GetValue); ¿why do you care about your neighbours?
¿why everybody is your brothers?
Last edited on
were those real questions?
its a binary search tree using a template
the value stored by bbst becomes a and int
it works just like vector<type>
it works just like vector<type>
No, it does not.
Any other container is not intrusive.

And you can have several containers, containing distinct items.

Still, you have an static method that receives an object of the class.
You have not seen the code yet and it does work I have been testing it
The reason why that method is static is because the function used to determine the number value of an object takes _TYPE and returns _VAL_TYPE, it could have been non-static outside of the class, it does not matter but it MUST have that type and parameter

Just because you don't understand templates does not mean its not possible
Last edited on
Topic archived. No new replies allowed.