开发者

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

开发者 https://www.devze.com 2022-12-30 20:02 出处:网络
Why I can\'t compile this code? #include <map> using namespace std; class MyTest { template<typename T> void test() const;

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

0

精彩评论

暂无评论...
验证码 换一张
取 消