0

After doing some research I have resigned to ask what is going wrong with std::sort.

Following Oliver Charlesworth advice from here I created a comparative, included the algorithm header and called std::sort in my function. I am still getting errors. Probably an oversight but still.

Errors:

Error C2780 void std::sort(_RanIt,_RanIt)': expects 2 arguments - 3 provided

Error C2672 'std::sort': no matching overloaded function found

Error C3867 'ImageEvaluator::comparator': non-standard syntax; use '&' to create a pointer to member

Struct:

struct numLocWidth
{
    int width;
    int number;
    int location;
};

Comparative:

bool comparator(const numLocWidth &a, const numLocWidth &b) 
{ 
    return a.location < b.location; 
}

Function:

if (tempMax > minAcceptableValue)
{
    tempLocAndVal.location = max_Pot_Loc.x;
    tempLocAndVal.number = i - 1;
    tempLocAndVal.width = templates[7][i].size().width;
    foundNumbers.push_back(tempLocAndVal);
    std::sort(foundNumbers.begin(), foundNumbers.end(), comparator);                
}

Not sure what is going on here and have been scratching my head for a bit. I could write my own sort function but I'm pretty sure this way will be more efficient.

Community
  • 1
  • 1
Paul Hashmi
  • 456
  • 5
  • 18
  • 4
    It looks like `comparator` is a non-static class member function. Make it static or non-member. – user7860670 Sep 30 '17 at 05:26
  • Please see [MCVE](https://stackoverflow.com/help/mcve). – Robert Prévost Sep 30 '17 at 05:28
  • 4
    You haven't provided enough information. From the error messages, it appears that your `comparator()` is a non-static member function of some class. The variant of `std::sort()` which accepts a comparator as the third argument requires that comparator to be a binary function (i.e. either a static member of a class, or a non-member function that accepts the required arguments). – Peter Sep 30 '17 at 05:28
  • 1
    Please see, https://stackoverflow.com/help/mcve – Jive Dadson Sep 30 '17 at 05:31
  • Thanks both of you. please put your answers up so I can mark as solved. – Paul Hashmi Sep 30 '17 at 05:36
  • Both VTT and Peter are correct I should of made static. – Paul Hashmi Sep 30 '17 at 05:37

1 Answers1

4

As VTT and Peter rightly pointed out, my comparative needed to be marked as static because it was a member of a class.

More information on this can be found here under requirements and BinaryPredicates.

Thanks for the help

Solution. From:

bool comparator(const numLocWidth &a, const numLocWidth &b) 
{ 
    return a.location < b.location; 
}

To:

static bool comparator(const numLocWidth &a, const numLocWidth &b) 
{ 
    return a.location < b.location; 
}

Thanks again for the help

Paul Hashmi
  • 456
  • 5
  • 18