Problems with converting from node* to node

Can someone please help me understand why my logic is false here? Much appreciated if so!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  class node {
public:
	node();
	node(int, node*);
	int get_element();
	node get_next();
private:
	int data;
	node* next;
};

node::node(int x, node* y) {
	data = x;
	next = y;
}
int node::get_element() {
	return data;
}
node node::get_next() {
	return next;  // Gives error: no suitable constructor exists to convert from "node*" to "node"
}
Presumably what you really want is
1
2
3
4
5
6
7
8
9
10
11
class node {
//...
	node *get_next();
//...
};

//...

node *node::get_next() {
	return next;
}
You want to return a pointer to the next node, not a copy of the next node.
You've defined your get_next() function to return a node object. However, you're trying to return next, which is a pointer, not an object.
I didn't notice that all, thank you very much @helios & @MikeyBoy!
You're welcome - glad it helped!
Topic archived. No new replies allowed.