vendredi 25 juin 2021

C++ priority queue with find function on derived data type

Following this question: https://stackoverflow.com/a/16749994/1703586 I am have a derived class from std::priority_queue. What I really need is a priority queue with a find() functionality.

When I try to use my_priority_queue I get a whole bunch of error which I do not understand (pasted below). With std::priority_queue, it compiles fine.

Please help me understand the compiler error and solve the issue.

Here is my standalone code:


#include <functional>
#include <queue>
#include <vector>
#include <iostream>

template<typename T>
void print_queue(T q) { // NB: pass by value so the print uses a copy
    while(!q.empty()) {
        auto x = q.top();
        auto cost = std::get<0>( x );
        auto s = std::get<2>( x );

        std::cout << "{ cost=" << cost << " | "  << std::get<0>(s) << "," << std::get<1>(s) << "," << std::get<2>(s) << "}";
        q.pop();
    }
    std::cout << '\n';
}


typedef int OcTreeKey ;
typedef bool NodeOccupancyStatus;
typedef std::tuple<OcTreeKey, int,  NodeOccupancyStatus > value_tt; //< key, depth, occupancy-enum

typedef std::tuple< float , value_tt, value_tt > dijstas_tuple; // cost to goal, parent of current, current node


template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class my_priority_queue : public std::priority_queue<T, Container, Compare>
{
public:
    typedef typename
        std::priority_queue<
        T,
        Container,
        Compare>::container_type::const_iterator const_iterator;

    const_iterator find(const T&val) const
    {
        auto first = this->c.cbegin();
        auto last = this->c.cend();
        while (first!=last) {
            if (*first==val) return first;
            ++first;
        }
        return last;
    }
};




int main() {


    // Using lambda to compare elements.
    auto cmp = [](dijstas_tuple left, dijstas_tuple right) { return ( std::get<0>(left) > std::get<0>(right) ); };
    // std::priority_queue<dijstas_tuple, std::vector<dijstas_tuple>, decltype(cmp)> q3(cmp);
    my_priority_queue<dijstas_tuple, std::vector<dijstas_tuple>, decltype(cmp)> q3(cmp);

    auto node_null = std::make_tuple( 2.0, -1, true );
    auto node_tup0 = std::make_tuple( 2.0, 14, true );
    auto node_tup1 = std::make_tuple( 12.0, 12, true );
    auto node_tup2 = std::make_tuple( 22.0, 12, true );
    auto node_tup3 = std::make_tuple( 51.0, 13, true );

    q3.push( std::make_tuple(8.0, node_null, node_tup0 ) );
    q3.push( std::make_tuple(3.0, node_tup2, node_tup1 ) );
    q3.push( std::make_tuple(7.0, node_tup2, node_tup2 ) );
    q3.push( std::make_tuple(10.0, node_tup1, node_tup3 ) );

    q3.pop();

    print_queue(q3);

}

Here is my compiler error:

custom_priority_queue.cpp: In function ‘int main()’:
custom_priority_queue.cpp:62:87: error: no matching function for call to ‘my_priority_queue<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> >, std::vector<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> > >, main()::<lambda(dijstas_tuple, dijstas_tuple)> >::my_priority_queue(main()::<lambda(dijstas_tuple, dijstas_tuple)>&)’
 eue<dijstas_tuple, std::vector<dijstas_tuple>, decltype(cmp)> q3(cmp);
                                                                     ^
custom_priority_queue.cpp:32:9: note: candidate: my_priority_queue<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> >, std::vector<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> > >, main()::<lambda(dijstas_tuple, dijstas_tuple)> >::my_priority_queue(const my_priority_queue<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> >, std::vector<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> > >, main()::<lambda(dijstas_tuple, dijstas_tuple)> >&)
 > class my_priority_queue : public std::priority_queue<T, Container, Compare>
         ^~~~~~~~~~~~~~~~~
custom_priority_queue.cpp:32:9: note:   no known conversion for argument 1 from ‘main()::<lambda(dijstas_tuple, dijstas_tuple)>’ to ‘const my_priority_queue<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> >, std::vector<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> > >, main()::<lambda(dijstas_tuple, dijstas_tuple)> >&’
custom_priority_queue.cpp:32:9: note: candidate: my_priority_queue<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> >, std::vector<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> > >, main()::<lambda(dijstas_tuple, dijstas_tuple)> >::my_priority_queue(my_priority_queue<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> >, std::vector<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> > >, main()::<lambda(dijstas_tuple, dijstas_tuple)> >&&)
custom_priority_queue.cpp:32:9: note:   no known conversion for argument 1 from ‘main()::<lambda(dijstas_tuple, dijstas_tuple)>’ to ‘my_priority_queue<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> >, std::vector<std::tuple<float, std::tuple<int, int, bool>, std::tuple<int, int, bool> > >, main()::<lambda(dijstas_tuple, dijstas_tuple)> >&&’

Aucun commentaire:

Enregistrer un commentaire