views:

347

answers:

1

I am trying to create a list object, with the iterator class nested inside to understand how it works. In some method, I am trying to return an iterator object but it doesn't work. I created an example to show the problem :

// CLASS A
template <class T>
class A
{
    public:
     class B;
     A(){}
};

// CLASS B
template <class T>
class A<T>::B
{
    private:
     int varB;

    public:
     B(B& b);
     B(const int&);
     B returnThis();
};

template <class T>
A<T>::B::B(const int& value)
{
    varB = value;
}

template <class T>
A<T>::B::B(B& b)
{
    varB = b.varB;
}

template <class T>
A<T>::B A<T>::B::returnThis()
{
    return *this;
}

// MAIN

void main()
{
    A<int>::B classB(10);
}

The error is near those lines:

template <class T>
A<T>::B A<T>::B::returnThis()

The compiler tells me I am missing a ; before A::B::returnThis()

I am trying to solve this problem for days and I can't find a way to make it work... I would really appreciate some help. Thanks in advance!

+4  A: 

You need typename:

typename A<T>::B

To indicate to the compiler that A<T>::B is a type. Here's a good explanation why.

What B is depends on what A<T> is, this is called dependency. Any time you are getting a type out of a class or struct, and it's dependent on a template, you'll need to use typename.

GMan
Thank you very much! I will have a look @ typename!
valli-R
No problem. ​:)
GMan