Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type/value mismatch in template C++ class declaration [duplicate]

Tags:

c++

templates

I am trying to compile the following code on Linux using gcc 4.2:

#include <map>
#include <list>

template<typename T>
class A
{
...

private:
    std::map<const T, std::list<std::pair<T, long int> >::iterator> lookup_map_;
    std::list<std::pair<T, long int> > order_list_;

};

When I compile this class I receive the following message from gcc:

error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
error:   expected a type, got ‘std::list<std::pair<const T, long int>,std::allocator<std::pair<const T, long int> > >::iterator’
error: template argument 4 is invalid

I have removed file names and line numbers , but they all refer to the line declaring the map.

When I replace the pair in these expressions with an int or some concrete type, it compiles fine. Can someone please explain to me what I am doing wrong.

like image 777
fido Avatar asked May 09 '09 12:05

fido


1 Answers

You need to write typename before std::list<...>::iterator, because iterator is a nested type and you're writing a template.

Edit: without the typename, GCC assumes (as the standard requires) that iterator is actually a static variable in list, rather than a type. Hence the "parameter type mismatch" error.

like image 150
Doug Avatar answered Sep 20 '22 05:09

Doug