For some reason this code is not accepted by the compiler.

template<class T, class A>
void ShowMap(const map<T, A>& v)
{
    for (map<T, A> ::const_iterator ci = v.begin();ci != v.end(); ++ci)
    cout << ci ->first <<": " << ci ->second <<endl;

    cout << endl;
}

The compiler says:

main.cpp:103: error: expected `;' before ‘int’
main.cpp:103: error: ‘ci’ was not declared in this scope
main.cpp:91:   instantiated from here
main.cpp:103: error: dependent-name ‘std::map<T,A,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::const_iterator’ is parsed as a non-type, but instantiation yields a type
main.cpp:103: note: say ‘typename std::map<T,A,std::less<_Key>,std::allocator<std::pair<const _Key, _Tp> > >::const_iterator’ if a type is meant
:: === Build finished: 5 errors, 0 warnings ===

Perhaps it doesn't like setting the itterator variable to v.begin()????

Thanks for any insights!

Recommended Answers

All 2 Replies

For some reason this code is not accepted by the compiler.

You have a good compiler! :) It accurately doesn't treat map<T, A>::const_iterator as a type because it relies on template parameters, T and A. To make the compiler believe you, you need to use the typename keyword.

template<class T, class A>
void ShowMap(const map<T, A>& v)
{
    for (typename map<T, A>::const_iterator ci = v.begin(); ci != v.end(); ++ci)
      cout << ci ->first <<": " << ci ->second <<endl;
    cout << endl;
}

The rule of thumb is that if any part of the type you're using depends on a template, you need to use typename to convince the compiler that it really is a type. ;)

You have a good compiler! :) It accurately doesn't treat map<T, A>::const_iterator as a type because it relies on template parameters, T and A. To make the compiler believe you, you need to use the typename keyword.

Thanks! That was the problem.You saved me alot of time trying to figure that one out!

Just for the record: I'm using the GNU/gcc compiler with Code::Blocks on Fedora Core 4 Linux, all the applications are much more up to date, however. Yum takes the pain out of updating!

Unfortunaly it appears that YUM is broken ( or at least buggy) in the FC6 release, so I'm going to hold off a while before upgrading...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.