Template question...

B

barcaroller

I have two template classes, where one uses the first for a list type.

template <typename T> class B
{
};


template <typename T> class A
{
typedef list< B<T> >::iterator MyIterator_t; // <--- error here
}


Compiler error: type std::list, std::allocator > > is not derived
from type A


Does anyone know what the error means and how to fix it?
 
J

Juha Nieminen

barcaroller said:
I have two template classes, where one uses the first for a list type.

template <typename T> class B
{
};


template <typename T> class A
{
typedef list< B<T> >::iterator MyIterator_t; // <--- error here
}


Compiler error: type std::list, std::allocator > > is not derived
from type A


Does anyone know what the error means and how to fix it?

It's fixed by adding the keyword 'typename' after the 'typedef'.

The reason is that the type definition uses the dependent name T, and
thus list< B<T> >::iterator being a type name has to be specified
explicitly.
 
A

Alf P. Steinbach

* barcaroller:
I have two template classes, where one uses the first for a list type.

template <typename T> class B
{
};

template <typename T> class A
{
typedef list< B<T> >::iterator MyIterator_t; // <--- error here
}


Compiler error: type std::list, std::allocator > > is not derived
from type A


Does anyone know what the error means and how to fix it?

The following compiles fine:

#include <list>

using namespace std;

template <typename T> class B
{
};

template <typename T> class A
{
typedef typename list< B<T> >::iterator MyIterator_t;
};


int main()
{
A<int>();
}

I added a semicolon and a word to your code.

Check the FAQ item on dependent names (assuming there is such a FAQ item, but
check -- it's nearly always a good idea to consult the FAQ :) ).


Cheers & hth.,

- Alf (helping others but would rather work, however, none in Norway want me)
 
R

red floyd

* barcaroller:








The following compiles fine:

     #include <list>

     using namespace std;

     template <typename T> class B
     {
     };

     template <typename T> class A
     {
         typedef typename list< B<T> >::iterator  MyIterator_t;
     };

     int main()
     {
         A<int>();
     }

I added a semicolon and a word to your code.

Check the FAQ item on dependent names (assuming there is such a FAQ item, but
check  --  it's nearly always a good idea to consult the FAQ :) ).

That's 35.19 and 35.20, and it doesn't discuss typename in this
context, alas. 35.19 really needs updating, or alternatively, a new
item should be added to section 35.
 
B

barcaroller

barcaroller said:
I have two template classes, where one uses the first for a list type.

template <typename T> class B
{
};


template <typename T> class A
{
typedef list< B<T> >::iterator MyIterator_t; // <--- error here
}


Compiler error: type std::list, std::allocator > > is not derived
from type A


Does anyone know what the error means and how to fix it?

Thank you all for your (correct) answers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top