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!
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
精彩评论