tags:

views:

44

answers:

1

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!

+6  A: 

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

Arkaitz Jimenez