louxiu
louxiu

Reputation: 2915

Compiles error when get container from std::priority_queue using subclass

I want to get the container from priority_queue using subclass, but the PQI_OK compiles ok, PQI_FAIL fails, why?

#include <queue>
#include <iostream>

class PQI_OK : public std::priority_queue<int> {
 public:
  std::vector<int>& GetContainer() { return c; }
};

template <class Tp, class Container, class Compare>
class PQI_FAIL : public std::priority_queue<Tp, Container, Compare> {

 public:
  Container GetContainer() {
    return c;
  }
};

int main(int argc, char *argv[])
{

  PQI_OK queue;
  queue.push(1);
  queue.push(2);

  for (auto it = queue.GetContainer().begin(); it != queue.GetContainer().end(); ++it) {
    std::cout << *it << std::endl;
  }

  return 0;
}

Errors:

tmp.cc:14:12: error: use of undeclared identifier 'c'
    return c;
           ^
1 error generated

Upvotes: 0

Views: 114

Answers (1)

rafix07
rafix07

Reputation: 20936

You need to use this-> to access data members from base class when base class depends on template parameter:

Container GetContainer() {
    return this->c; // error: use of undeclared identifier 'c'
  }

Upvotes: 2

Related Questions