I am trying to implement a priority queue. This is the code for the interface class:
template<typename TYPE, typename COMP_FUNCTOR = std::less<TYPE>>
class priority_queue {
public:
typedef unsigned size_type;
virtual ~priority_queue() {} // impleted but does nothing, this is not a pure virtual function
virtual void fix() = 0;
/* some other methods*/
protected:
COMP_FUNCTOR compare;
};
And the code that causes problem:
template<typename TYPE, typename COMP_FUNCTOR = std::less<TYPE>>
class sorted_priority_queue : public priority_queue<TYPE, COMP_FUNCTOR> {
public:
typedef unsigned size_type;
template<typename InputIterator>
sorted_priority_queue(InputIterator start, InputIterator end, COMP_FUNCTOR comp = COMP_FUNCTOR());
/* some other methods*/
private:
std::vector<TYPE> data;
};
template<typename TYPE, typename COMP_FUNCTOR>
template<typename InputIterator>
sorted_priority_queue<TYPE, COMP_FUNCTOR>::sorted_priority_queue(
InputIterator start, InputIterator end, COMP_FUNCTOR comp) {
for(auto it = start; it != end; ++it) {
data.push_back(*it);
}
fix();
this->compare = comp; // <--- the line that causes problem
}
When I tried to do compare = comp at the last line, it says "use of undeclared identifier 'compare'" and I must declare this-> in order to access compare, which is a protected member variable defined in the interface class, why? Thank you.