Tuesday, 12 September 2017

type/value mismatch in template C++ class declaration




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



#include 

#include

template
class A
{
...

private:
std::map >::iterator> lookup_map_;
std::list > 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 std::map’
error: expected a type, got ‘std::list,std::allocator > >::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.


Answer



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.


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...