0

First parametrized class

template<typename T>
class elements
{
    protected:
        int e,f;
    public:
        elements(){}
        elements(int ee):e(ee){}
        const elements& operator=(const elements& other)
        {
            e = other.e; return *this;
        }

};

The base class

template<typename T>
class list
{
    protected:
        T item;
};

Finally the class in contention

template<typename T>
class row:public list<elements<T> >
{
    public:
        row(int a)
        {
            item;
        }
};

if the parameter is specified as int, list etc. Then the program runs else I keep getting the error 'item' was not declared in this scope,

I can't seem to get to work and would appreciate if someone could help me understand how it works.

Thanks

2 Answers2

0

You just need to specify the correct scope:

template<typename T>
class row:public list<elements<T> >
{
    public:
        row(int a)
        {
            list<elements<T> >::item;
         // ^^^^^^^^^^^^^^^^^^^^
        }
};

See Demo

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

The short answer is that the compiler doesn't know what item is. Depending on what T is, it could be a type, a variable. Because it can't tell without more information, it checks to see if it's something immediately available without the templated information and because it's not, the compiler has to give up.

By saying this->item instead of just item, you are very clearly telling the compiler that item is a variable.

The answer in the question linked in the comments is quite good and worth a read.

As you can see here, both this-> and the class scope both work: https://godbolt.org/g/Pc8oLn

xaxxon
  • 19,189
  • 5
  • 50
  • 80