must use '.*' or '->*' to call pointer-to-member function in 'lessThan (...)', e.g. '(... ->* lessThan) (...)'
Asked Answered
S

2

5

I am trying to sort my QList based on a QDateTime but I get the following error:

must use '.*' or '->*' to call pointer-to-member function in 'lessThan (...)', e.g. '(... ->* lessThan) (...)'
 if (lessThan(*end, *start))
                          ^

Sort function:

bool sortRecord(Record left, Record right){
    return left.getArrival().getDate() < right.getArrival().getDate();
}

Function is called like this:

qSort(recordList.begin(), recordList.end(), sortRecord);

Getter and setter of arrival in Record:

void Record::setArrival(Arrival arrival){
    this->arrival = arrival;
}
Arrival Record::getArrival(){
    return this->arrival;
}

getDate() function in Arrival:

QDateTime Arrival::getDate(){
    QDateTime qDateTime;

    QDate qDate;
    qDate.setDate(date.getDateYear(), date.getDateMonth(), date.getDateDay());
    qDateTime.setDate(qDate);

    vector<string> timeS = splitTime(time.getTimeFrom());

    QTime qTime;
    qTime.setHMS(stoi(timeS[0]), stoi(timeS[1]), 0);
    qDateTime.setTime(qTime);

    return qDateTime;
}

What is it that I do wrong?

Thanks!

Scintilla answered 25/11, 2015 at 9:52 Comment(5)
Where is lessThan used, where is it declared?Cottonweed
The function lessThan is declared inside the qalgorithms.h that is part of the QT framework.Scintilla
So where/how do you invoke the sort?Cottonweed
My guess is that sortRecord is a non-static member function, which you're passing to Qt's sorting function. Make it static, or liberate it entirely from the shackles of the class system.Lavellelaven
Please don't add unnecessary off-site links. Questions such as this one should be self-contained. The entirety of your problem can be illustrated in about 10 lines of self-contained main.cpp - that's what should have been in your question to begin with. Your job is to throw out everything that doesn't contribute to the problem: it's just noise.Back
H
6

The problem is here:

qSort(recordList.begin(), recordList.end(), sortRecord);
                                            ^^^^^^^^^^

You cannot use a non-static member function as the sort function, because a non-static member function needs to be called on some object (to provide the this pointer). You can't just call member function like a normal function, which is what the compiler error means. If you'd read the whole error message, not just the first line, then it would have told you that it comes from the line above.

Either make the sortRecord function a non-member function, or make it a static member function.

Why is it a member function anyway? It doesn't access *this, or use any private members ... this smells like bad object oriented style, that's not how we do things in C++ (see e.g. How non-member functions increase encapsulation).

Also why does your sortRecord function copy its arguments instead of using references? (See https://isocpp.org/wiki/faq/references#call-by-reference)

If you want to write everything as a member function and have pass-by-reference semantics then use Java, not C++. Otherwise, stop writing Java code in C++.

Haematothermal answered 25/11, 2015 at 11:17 Comment(1)
You might want to tone down. OP is clearly new to C++. You could've posted some suggestive feedback instead of flaming him for trying. We all were new to something once, even you, Einstein.Hut
W
1

Try this function for sorting.

bool sortRecord(const Record& left, const Record& right)
{ 
    return left.getArrival().getDate() < right.getArrival().getDate(); 
}

And also make sure that getArrival() and getDate() are const methods.

Whippletree answered 25/11, 2015 at 10:16 Comment(1)
Although that is a good suggestion, it doesn't solve the problem.Haematothermal

© 2022 - 2024 — McMap. All rights reserved.