Krzysztof Tarnawski
Krzysztof Tarnawski

Reputation: 5

C++ Qt QtConcurrent::filteredReduced from QVector of std::shared_ptr

I have a vector of shared_ptrs of my class Person, which looks like:

QVector <std::shared_ptr<const Person>> vecOfPeople;

One of Person's field is age and I want to calculate with QtConcurrent::filteredReduced for example how many there are people over 50 and I find it very difficult to understand how to do it. I have a bool returning function isOver50 as:

bool isOver50(std::shared_ptr<const Person> &person)
{
    return person->getAge() > 50;
}

And if I understand good, there should be also a reduction function, which at my code looks like:

void reduction(int &result, std::shared_ptr<const Person> &person)
{
    result++;
}

And finally, code with filteredReduced as:

QFuture<int> futureOver50 = QtConcurrent::filteredReduced(vecOfPeople, isOver50, reduction);

futureOver50.waitForFinished();

qDebug() << futureOver50.result();

This doesn't compile, my bet is there's something wrong in reduction function, but I have no idea what it is.

Upvotes: 0

Views: 373

Answers (1)

hlt
hlt

Reputation: 6317

From the Qt documentation:

The filter function must be of the form:

bool function(const T &t);

The reduce function must be of the form:

V function(T &result, const U &intermediate)

Your shared_ptr arguments are non-constant references (even though the pointed-to type is constant) where Qt wants to pass a constant reference, leading to a compilation error.

Instead, consider using

bool isOver50(const std::shared_ptr<const Person> &person);
void reduction(int &result, const std::shared_ptr<const Person> &person);

In the future, please try to submit the actual error message along with your question, it makes diagnosing these issues a lot faster

Upvotes: 2

Related Questions