Adam Adam - 1 year ago 127
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);
return (Node1->ID > Node2->ID);

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download