Class problem c++

Pages: 12
Ah... it's because you're trying to treat a temporary variable as if it's an lvalue:

&(*node)->getleft()

Passing the address of a temporary variable into your insert function is invalid. If it was allowed, then by the time the insert function operates on that address, it will no longer be pointing to valid memory.

EDIT: I love the way MS translate the text of the error message, but then leave the word "with" in English :D
Last edited on
Uhm, then i must create a new methods Gets (left and right) for the insert?
How can i fix it?
Last edited on
I'm not really sure what you're trying to do. Why does insert need a pointer to a pointer?
Because Insert funciton call the methods newNode for create a new node
As this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
template <typename T>
BinaryNode<T>* BinarySearchTree<T>::newNode(T key)
{
	BinaryNode<T>* node = new BinaryNode<T>;
	node->setkey(key);
	node->setleft(NULL);
	node->setright(NULL);
	node->setparent(NULL);

	return node;
}
template <typename T>
void BinarySearchTree<T>::insert(T key)
{
	this->insert(&this->root, key);
}
template <typename T>
void BinarySearchTree<T>::insert(BinaryNode<T>** node, T key)
{
	if (*node == NULL)
	{
		*node = this->newNode(key);
		cout << "-Creating... " << (*node)->getkey() << endl << endl;
	}
	else
	{
		if (key <= (*node)->getkey())
		{
			cout << "<-Left" << endl;
			insert(&(*node)->getleft(), key);
			(*node)->getleft()->setparent(*node);
		}
		else
		{
			cout << "Right->" << endl;
			insert(&(*node)->getright(), key);
			(*node)->getright()->setparent(*node);
		}
	}
}
OK... so the BinaryNode<T>** that the calling code passes in must contain an address that can actually be used to store the value. &(*node)->getright() isn't a valid address, because the return value from getleft() is held in a temporary variable. The address of that temporary variable is of no use to you, because, well, it's temporary.

Are you trying to do it this way because you want to insert the new node at the left position? If so, you could do something like:

1
2
3
4
BinaryNode<T>* tmpNodePtr;

insert(&tmpNodePtr, key);
(*node)->setLeft(tmpNodePtr);

This assumes that the setLeft() method simply sets the value of this->left to the value that was passed in. If you're doing something weird with your setLeft() method, then that might not do what you want.

Are you familiar with references? This would be a lot simpler if you used them instead:

1
2
3
4
5
6
BinaryNode<T>*& getleftRef(){ return left; }

template <typename T>
void BinarySearchTree<T>::insert(BinaryNode<T>*& node, T key);

insert((*node)->getleft(), key);


Although returning a non-const reference to a data member is breaking encapsulation, so that might not be a good idea.

Last edited on
The second way not working
I think because isn't correct here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template <typename T>
void BinarySearchTree<T>::insert(T key)
{
	this->insert(*&this->root, key);
}
template <typename T>
void BinarySearchTree<T>::insert(BinaryNode<T>*& node, T key)
{
	if (*node == NULL)
	{
		*node = this->newNode(key);
		cout << "-Creating... " << (*node)->getkey() << endl << endl;
	}
	else
	{
		if (key <= (*node)->getkey())
		{
			cout << "<-Left" << endl;
			insert((*node)->getleft(), key);
			(*node)->getleft()->setparent(*node);
		}
		else
		{
			cout << "Right->" << endl;
			insert((*node)->getright(), key);
			(*node)->getright()->setparent(*node);
		}
	}
}



The first way, instead, crash

Can we solve all with operator overloading? if yes, how can i do it?
There's all file, can you test it?
https://www.dropbox.com/s/s8xsn3q01ysyy4f/classi.h
https://www.dropbox.com/s/3qw7od6fysa2mr9/function.cpp
https://www.dropbox.com/s/atd4tif0tntz9to/header.h
https://www.dropbox.com/s/dgh5pwvsd6jozpw/main.cpp
Last edited on
First way, instead, the program crash:
i use this:
1
2
BinaryNode<T>* temp = (*node)->getleft();
insert(&temp, key);


But the program crash..... :(
This is where you fire up your trusty debugger so that you can find out where it's crashing, and what the state of the memory is at that point.
Ok, i've resolved the problem, i hope...
I put the methods insert and newNode inside the BinaryNode class, and i use the attributes directly
Topic archived. No new replies allowed.
Pages: 12