Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use comparer function inside class with algorithm header?

This is what my compare function looks like:

bool smallest_weight(const size_t& i, const size_t& j) {
    return this->abs_weight[i] < this->abs_weight[j];
}

I use this function inside the constructor of the class to initialize some other arrays. This is the code that uses it:

size_t best_node = *min_element(
    this->pointer_list[i + 1].begin(),
    this->pointer_list[i + 1].end(),
    smallest_weight
);

When I try to compile, I get the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error: invalid use of non-static member function ‘bool TimeCalculator::smallest_weight(const size_t&, const size_t&)’

I can’t make the function static since it won’t be able to access the data inside the class, I’d also like to avoid making the arrays global if possible.

How can I achieve that?

>Solution :

Try this:

size_t best_node = *min_element(
    this->pointer_list[i + 1].begin(),
    this->pointer_list[i + 1].end(),
    [&](const auto& i, const auto& j) noexcept { return this->smallest_weight(i, j); }
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading