0

In my project I have some structure like this:

template <typename T>
class deque
{
// ...
    class MyIterator
    {
    protected:
        T* p;

    public: 
        MyIterator(T* x)
        : p(x)
        {
        }
    };

    class Iterator: public MyIterator
    {
    public: 
        Iterator(T* x)
        : MyIterator(x)
        {
        }

        T* foo() { return p; }
    };
};

I keep getting an error

`In member function 'foo' error: ‘p’ was not declared in this scope
         return p`

I've tried to change p from protected to public, but this did not help. What am I doing wrong?

p.i.g.
  • 2,815
  • 2
  • 24
  • 41
PepeHands
  • 1,368
  • 5
  • 20
  • 36
  • 2
    Did you try `return this->p;` ? – Slava May 21 '15 at 21:26
  • @Slava it worked! thanks! Can you sir, please, pos this as an answer and I'll accept this. And please, could you explain why `this->` needed? I didn't need this in some regular classes with inherit – PepeHands May 21 '15 at 21:30
  • I tried to compile your code and it worked fine without any modification (Windows 8, VS 2013). – p.i.g. May 21 '15 at 21:33
  • @vizhanyolajos: That's because VS plays fast and loose with the name lookup rules. A compiler following the language standard wouldn't find a name in a context that depends on the value of a template parameter, since that value isn't known until the template is instantiated. – Mike Seymour May 21 '15 at 22:13

0 Answers0