使用子类从 std::priority_queue 获取容器时编译错误

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

我想使用子类从 priority_queue 获取容器,但是 PQI_OK 编译正常,PQI_FAIL 失败,为什么?

#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;
}

错误:

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

当基 class 依赖于模板参数时,您需要使用 this-> 从基 class 访问数据成员:

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