lundi 22 juin 2015

Sorting vector of Pointers of Custom Class

I have vector<FPGA*> current_generation_, which I'd like to sort by FPGA member fitness_ using the sort_members function. Applicable code follows:

bool sort_members (FPGA* fpga_first, FPGA* fpga_second) {
    return (fpga_first->fitness() < fpga_second->fitness());
};

fpga.hpp

#include <vector>

class FPGA {
    public:
        explicit FPGA(int input_gates, int output_gates, int normal_gates);

        const int fitness();

    protected:
        int fitness_;
};

fpga.cpp

FPGA::FPGA() {
    this->fitness_ = 0;
}

const int FPGA::fitness() {
    return this->fitness_;
}

implementation:

std::sort(this->current_generation_.begin(), this>current_generation_.end(), sort_members);

errors:

/usr/include/c++/4.9/bits/stl_algo.h: In instantiation of ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::__detail::_Node_iterator<std::pair<const int, FPGA*>, false, false>; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(FPGA*, FPGA*)>]’:
/usr/include/c++/4.9/bits/stl_algo.h:4717:78:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = std::__detail::_Node_iterator<std::pair<const int, FPGA*>, false, false>; _Compare = bool (*)(FPGA*, FPGA*)]’

/usr/include/c++/4.9/bits/stl_algo.h:1968:22: error: no match for ‘operator-’ (operand types are ‘std::__detail::_Node_iterator<std::pair<const int, FPGA*>, false, false>’ and ‘std::__detail::_Node_iterator<std::pair<const int, FPGA*>, false, false>’)
     std::__lg(__last - __first) * 2,

The remainder of the total error string is huge, but I believe is mostly what the compiler believes(falsely) are candidates. I'm not extremely familiar with c++, and a compiler error of this magnitude and complexity is confusing to me.

I can provide more context if needed. Thanks!

Aucun commentaire:

Enregistrer un commentaire