The return type of your getSmallest () function is defined as a Node object, which in C ++ means that the return expression must be of Node type, and at run time the memory of the returned object will be copied back to the caller.
Since you cannot return the integer 0.
Instead, you can define a specific instance object for a Node that represents a NULL Node. This mainly depends on the definition of Node, assuming the following:
class Node {
You can define a NULL Node in the same way as:
class Node {
The above example assumes that you really never set field a to -1 in other Node objects. If -1 does not match your goal, you can choose a value that is supposed to never be used. If you have multiple fields in Node, you can represent NULL_NODE with a combination of values.
EDIT: As innosam points out, you can also (and probably better) add a boolean field to the Node class to represent if Node is NULL or.
Using the above, you can implement your function as follows:
Node getSmallest() { if (openList.empty()) return Node.NULL_NODE;
Otherwise, you can use a third-party tool that allows you to do the same. See other people's answers in this case.
source share