0

I have a class:

template<typename T, typename Alloc = std::allocator<T>>
class CCircularBuffer {
 protected:
  size_t size_ = 0;
  size_t max_size_{};
  T* values_;
  size_t begin_ = 0;
  size_t end_ = 0;
  Alloc alloc_;

 public:
  virtual void resize(size_t size);
};

And the class that inherits it:

template <typename T, typename Alloc>
class CCircularBufferExt: public CCircularBuffer<T, Alloc> {
 public:
    void resize(size_t n);

};

But when I try to use fields from the CCircularBuffer class, the compiler does not see them:

Error: Use of undeclared identifier 'values_'

template<typename T, typename Alloc>
void CCircularBufferExt<T, Alloc>::resize(size_t n) {
    values_ = alloc_.allocate(n);
}

What could be the problem? And am I using inheritance correctly?

I thought the problem was that the compiler doesn't see the CCircularBuffer class, but it seems to see it anyway...

Error screenshot: enter image description here

teddy_bear
  • 13
  • 3
  • I am not sure why, but explicitly referring to the base class solves it: `CCircularBuffer::values_ = CCircularBuffer::alloc_.allocate(n);` – nielsen Mar 07 '23 at 09:55
  • 2
    @nielsen It's because templates are involved. You can have `CCircularBuffer` specialization that has different set of members, so compiler doesn't just assume unqualified name to be a member. FWIW, `this->values_` works just as well (or a `using` declaration), so you don't have to explicitly spell out base class name every time. – Yksisarvinen Mar 07 '23 at 09:58
  • @Yksisarvinen Thanks, that makes sense. You should write an answer. – nielsen Mar 07 '23 at 10:00

0 Answers0