class member call.

hi,
I'm creating a binary search tree.

class code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class bstnode{

private:
	int key;
	bstnode *left;
	bstnode *right;

public:
	//bstnode
	bstnode()
		{key=-1; left=0; right=0;}
//set right
	void set_right(bstnode *r)
		{right = r;}


main code:
1
2
3
4
5
//declare a bstnode
bstnode(node2);

//set right of node
	node.set_right(node2);


i get erreor below:

IntelliSense: no suitable conversion function from "bstnode" to "bstnode *" exists

how can i solve it?

Thank you,
set_right() takes a pointer as its argument, but you are passing an object.

You need to give the function node2's address in memory:

node.set_right(&node2)
xish,
i did it,
when i printed node.right
it gave me the address of node.right not the key of node2
how can i see the key of node2 ?
thank you,
bstnode *right is a pointer to another object.

You can access private members of another object by using the arrow operator:

right->key

The arrow operator is similar to the dot operator, except it implicitly derefrences the pointer on the LHS before accessing its members. The above statement is equivalent to:

(*right).key

Read more here: http://www.cplusplus.com/doc/tutorial/structures/
Last edited on
Thank you, xish.
it worked.
Try declaring the nodes as pointers

bstnode *node2
bstnode *node

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char** argv) 
{
	//declare a bstnode
	bstnode *node2, *node;          //-----------> Here!!!!!!!!
	 

	//set right of node
	node->set_right(node2);
	
	std::cin.clear();
	std::cin.get();
	
	return 0;
}


Hope it works!!!
if i declare node as pointer then this one doesn't work.

node->get_right().key ;

also this one.

node.get_right() ;

also when I'm going to use them as you said, i reach the error below,

The variable 'node2' is being used without being initialized.

actually it became the problem,

the first node (node) is not a pointer and i can use it,
the second node (node2) in a pointer and can be used for node,
But! when I'm going to set the right of node2 here there is a problem

The variable 'node2' is being used without being initialized. (again)

it doesn't get any value as key or any other pointer as right or left,

so if pointer does not get pointer,
and only class substance gets pointer,
and class substance doesn't get other class substances,

then how am i suppose to grow my binary search tree?
Do you mind sharing the code snip-outs so I can help out??
Topic archived. No new replies allowed.