1

I am trying to sort a vector by passing the first and last item and passing a bool predicate. Cannot for the life of me figure out what i am doing wrong. Suspect its lack of understanding of classes. But its really got me stumped.

Am going through accelerated c++ and am on chapter 4. Spent a good while reading around but am not getting anywhere. Probably doesn't help me that i am doing things in classes either so i can keep chapters side by side.

The error i am getting is

error C2064: term does not evaluate to a function taking 2 arguments

on the line that calls sort.

So in a header (Ch4) i have the following (I have edited this code down for your sanity - the includes etc seem to be happy - the header and code are in separate files and am using vs2013 so its sorted all that out for me)

class Ch4
{
public:
    int Ch4::Run();
    struct Student_Info;
    bool compare(const Student_Info& x, const Student_Info& y);
}

Then in the class:

struct Ch4::Student_Info
{
    string name;
    double midterm, final;
    vector<double> homework;
};

int Ch4::Run()
{
    vector<Student_Info> students;
    ... code that populates it
    sort(students.begin(), students.end(), compare);
}

bool Ch4::compare(const Student_Info& x, const Student_Info& y)
{
    return x.name < y.name;
}

when i change that line to

sort(students.begin(), students.end(), Ch4::compare);

I get an error saying its missing an argument list - but in my list of overloads it doesn't show one that takes an argument list.

So i follow its advice and use it as a reference and i get back to the original error message.

So I am confused about 2 things: 1) why doesn't my code work - how to fix it 2) What are these error messages telling me, why do they seem to talk about an overload that doesn't exist or is hidden from me?

John Nicholas
  • 4,778
  • 4
  • 31
  • 50

2 Answers2

1

The problem is that your compare function is non-static. You either need to make it a free function, or else make it static.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
1

compare is a non-static method

Make it static:

static bool compare(const Student_Info& x, const Student_Info& y);
Jarod42
  • 203,559
  • 14
  • 181
  • 302