Adam Adam - 2 months ago 21
C++ Question

Calling C++ std::sort on a vector of struct pointers

*edited to add error messages

Visual Studio's intellisense is red flagging the sort function when I call it on a vector of pointers to a struct that I've defined earlier in the program.

I've instantiated a function pointer to my comparison method and am calling sort as follows:

bool(*compareNodes)(nodePtr, nodePtr) = compNodes;

sort(frontier.begin(), frontier.end(), compNodes);

/* flags compnodes and sort; "cannot determine which instance of overloaded
* function "compNodes is intended" */


compNodes function:

static bool compNodes(nodePtr Node1, nodePtr Node2){
if (Node1->fValue != Node2->fValue)
return (Node1->fValue < Node2->fValue);
else
return (Node1->ID > Node2->ID);
}

Answer

Wild guess: compNodes is overloaded. You smartly tried getting a pointer to the right overload with compareNodes, but you're not actually using it in std::sort(). Change the line to:

sort(frontier.begin(), frontier.end(), compareNodes);

If your warning level was high enough, you'd see that compareNodes is currently unused.

Comments