I'm trying to create a functional inheritance hierarchy. We are working with implementing our own priority queue class template, and the program includes lists such as buy orders.
My idea was to have "p_queue" as the base class, and let the "orders" class inherit from it, and then let subclasses such as "buy_orders" inherit from the orders class. My reasoning is that every order in this program is a priority queue, as they will be sorted by it.
Normally I would understand the error "type 'base_class' is not a direct base of 'sub_class'" if the derived class wasn't directly inheriting the superclass. But since the orders class inherits the p_queue class and no other class directly inherits from priority queue class, I'm confused why I get this error message.
Here is the code
//main.cpp #include "orders.h" int main(){ return 0; } //orders.h #include "p_queue.h" template<typename T> struct less{ bool operator()(const T& a, const T& b){ return a < b; } }; template<typename T> class Orders : public p_queue<T, decltype(less<T>())> { private: size_t m_Size; std::vector<T> list; public: Orders(const size_t& sz, const std::vector<T>& l) : list(l), m_Size(sz), p_queue<T>(&(*list.begin()), &(*list.end()), less<T>()){} virtual const size_t getSize() const { return m_Size; } virtual const std::vector<T> getList() const = 0; }; struct buy_orders : public Orders<size_t>{ std::vector<size_t> buy_prices; buy_orders(const size_t& sz) : Orders(sz, buy_prices) {} }; //p_queue.h #include <functional> template<typename T, typename Compare = std::less<T>> class p_queue{ protected: T* m_first; T* m_last; Compare m_comp; public: p_queue(T* first, T* last, Compare comp = Compare()) : m_first(first), m_last(last), m_comp(comp) {} }; The above code produces the error:
<source>: In instantiation of 'Orders<T>::Orders(const size_t&, const std::vector<T>&) [with T = long unsigned int; size_t = long unsigned int]': <source>:39:57: required from here <source>:31:59: error: type 'p_queue<long unsigned int, std::less<long unsigned int> >' is not a direct base of 'Orders<long unsigned int>' 31 | p_queue<T>(&(*list.begin()), &(*list.end()), less<T>()){} | ^ <source>:31:59: error: no matching function for call to 'p_queue<long unsigned int, less<long unsigned int> >::p_queue()' <source>:13:5: note: candidate: 'p_queue<T, Compare>::p_queue(T*, T*, Compare) [with T = long unsigned int; Compare = less<long unsigned int>]' 13 | p_queue(T* first, T* last, Compare comp = Compare()) : | ^~~~~~~ <source>:13:5: note: candidate expects 3 arguments, 0 provided <source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(const p_queue<long unsigned int, less<long unsigned int> >&)' 7 | class p_queue{ | ^~~~~~~ <source>:7:7: note: candidate expects 1 argument, 0 provided <source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(p_queue<long unsigned int, less<long unsigned int> >&&)' <source>:7:7: note: candidate expects 1 argument, 0 provided ASM generation compiler returned: 1 <source>: In instantiation of 'Orders<T>::Orders(const size_t&, const std::vector<T>&) [with T = long unsigned int; size_t = long unsigned int]': <source>:39:57: required from here <source>:31:59: error: type 'p_queue<long unsigned int, std::less<long unsigned int> >' is not a direct base of 'Orders<long unsigned int>' 31 | p_queue<T>(&(*list.begin()), &(*list.end()), less<T>()){} | ^ <source>:31:59: error: no matching function for call to 'p_queue<long unsigned int, less<long unsigned int> >::p_queue()' <source>:13:5: note: candidate: 'p_queue<T, Compare>::p_queue(T*, T*, Compare) [with T = long unsigned int; Compare = less<long unsigned int>]' 13 | p_queue(T* first, T* last, Compare comp = Compare()) : | ^~~~~~~ <source>:13:5: note: candidate expects 3 arguments, 0 provided <source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(const p_queue<long unsigned int, less<long unsigned int> >&)' 7 | class p_queue{ | ^~~~~~~ <source>:7:7: note: candidate expects 1 argument, 0 provided <source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(p_queue<long unsigned int, less<long unsigned int> >&&)' <source>:7:7: note: candidate expects 1 argument, 0 provided If I remove the buy_orders struct from orders.h, the problem disappears. Why is this? How can I solve this issue?
std::less. So this is ambiguous.p_queue<T>is not a direct base ofOrderswhilep_queue<T, decltype(less<T>())>is a direct base. And since you reopened the question, I've explained the reason in my answer.p_queue<T, decltype(less<T>())>but trying to use the ctor ofp_queue<T>and hence the error. Basically,p_queue<T, decltype(less<T>())>andp_queue<T>are different class types. The ctor that you use must belong top_queue<T, decltype(less<T>())>. The solution is given in my answer below.