I get a code for Fibonacci Heap. This code uses the follow function to compare two keys:
int
cmp(void *x, void *y)
{
int a, b;
a = (int)x;
b = (int)y;
if (a < b)
return -1;
if (a == b)
return 0;
return 1;
}
This works because the KEY is currently a INT number.
I would like to adapt this code to use my class called “Node”. This class have implemented the operator <. >, <=, == and >=.
My adaptation in the code was:
Node a, b // instead int a, b
a = (Node)x;
b = (Node)y;
But I get the error:
dijkstra.cpp:168: error: no matching function for call to 'Node::Node(void*&)'
graph.h:39: note: candidates are: Node::Node()
graph.h:39: note: Node::Node(const Node&)
I tried also:
Node a, b // instead int a, b
a = (Node*)x;
b = (Node*)y;
and get the error:
dijkstra.cpp:168: error: no match for 'operator=' in 'a = (Node*)x'
graph.h:39: note: candidates are: Node& Node::operator=(const Node&)
I give up to try set the value and solve the question as a follow:
int cmp(void *x, void *y)
{
if ((Node*)x < (Node*)y)
return -1;
if ((Node*)x == (Node*)x)
return 0;
return 1;
}
I would like to understand what I am doing wrong in the previous examples.
Thanks in advance.
Change the following lines
to
You were merely type casting the pointers from
void*toNode*and trying to assign the pointer to a variable of typeNode.