Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derived class cannot see base class member [duplicate]

What am I doing wrong in this code?

template <typename T>
class CLASS1
{
public:
    T member;
};

template <typename T>
class CLASS2 : public CLASS1<T>
{public:
    void func()
    {

        member = 4;
    }
};

Visual Studio error:

member identifier not found

G++ error:

‘member’ was not declared in this scope

like image 362
Zebrafish Avatar asked Sep 15 '26 16:09

Zebrafish


1 Answers

You need to specify where the name member comes from. In this case, it comes from the inherited class template CLASS1<T>, so you need to say:

void func()
{
  CLASS1<T>::member = 4;
}

If you say this->member, then the compiler knows to look for names in the base classes as well. So you could do:

void func()
{
  this->member = 4;
}
like image 87
cigien Avatar answered Sep 18 '26 04:09

cigien