-1

Problem with inheritance and templates my derived class doesn't recognize x which is a member variable in my base class.

 template <class type>
 class one
 {
 public:
 type getX();
 type x;
 };

 template <class type>
 type one<type>::getX()
 {
 return x;
 }



 template <class type>
 class two: public one <type>
 {
 public:
 type getX();
 };

 template <class type>
 type two<type>::getX()
 {
   return x;
 }
aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 1
    possible duplicate of [Accessing inherited variable from templated parent class](http://stackoverflow.com/questions/605497/accessing-inherited-variable-from-templated-parent-class) – Mgetz Jul 25 '13 at 18:36
  • See also: http://www.parashift.com/c++-faq/nondependent-name-lookup-members.html – aschepler Jul 25 '13 at 19:15
  • why does this awful qst come up as the #2 search result on google? –  Mar 02 '14 at 03:59

2 Answers2

1

Since two is a template, and x is not direct a member of two, you need to make explicit the dependency.

One way is one<type>::x, another can be this->x.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
0

You have to use "this->" for accessing template base class member & functions inside template parent class.

return this->x;