yegor256
yegor256

Reputation: 105213

Simple map<> instantiation, I can't compile, please help

Why I can't compile this code?

#include <map>
using namespace std;
class MyTest {
    template<typename T> void test() const;
};
template<typename T> void MyTest::test() const {
    map<string, T*> m;
    map<string, T*>::const_iterator i = m.begin();
}

My compiler says:

In member function ‘void MyTest::test() const’:
test.cpp:8: error: expected `;' before ‘i’

What is it about? Many thanks in advance!

Upvotes: 0

Views: 219

Answers (1)

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23198

You have a dependent name, you need to append typename to the const_iterator because its type depends on the type of T.

template<typename T> void MyTest::test() const {
    map<string, T*> m;
    typename map<string, T*>::const_iterator i = m.begin();
}

C++ faq on depenent names

Upvotes: 6

Related Questions