c++ - Accessing base member data error when derived class is templated -
i have following problem curiously recurring template, problem when try access data member of crtp base class.
template<typename t> struct base { int protecteddata=10; }; struct derived : public base<derived> { public: void method() { std::cout<<protecteddata<<std::endl; }; }; int main () { derived a; a.method(); } the above code compiles , runs fine , can "10" printed, if have derived class templated, like:
template<typename t> struct base { int protecteddata=10; }; template<typename t> struct derived : public base<derived<t> > { public: void method() { std::cout<<protecteddata<<std::endl; }; }; class a{}; int main () { derived<a> a; a.method(); } class dummy class served template parameter. compiler complains cannot find "protecteddata". error information following:
g++-4.9 test.cc -wall -std=c++1y -wconversion -wextra test.cc: in member function ‘void derived<t>::method()’: test.cc:26:11: error: ‘protecteddata’ not declared in scope cout<<protecteddata<<endl;
it doesn't have crtp, rather fact dependent-base-accessing derived code, need qualify things.
changing line to
std::cout<<this->protecteddata<<std::endl; solved it.
Comments
Post a Comment