Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected member is "not declared in this scope" in derived class [duplicate]

#include <vector> #include <iostream>  template <class T> class Base { protected:     std::vector<T> data_; };  template <class T> class Derived : public Base<T> { public:     void clear()     {         data_.clear();     } };  int main(int argc, char *argv[]) {     Derived<int> derived;     derived.clear();     return 0; } 

I cannot compile this program. I get:

main.cpp:22: error: 'data_' was not declared in this scope 

Please, could you explain why data_ is not visible in the Derived class?

like image 264
Martin Drozdik Avatar asked Aug 20 '12 04:08

Martin Drozdik


1 Answers

To fix this, you need to specify Base<T>::data_.clear() or this->data_.clear(). As for why this happens, see here.

like image 157
Yuushi Avatar answered Sep 16 '22 23:09

Yuushi