简单的map <>实例化,我无法编译,请帮忙

yeg*_*256 0 c++

为什么我无法编译此代码?

#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();
}
Run Code Online (Sandbox Code Playgroud)

我的编译器说:

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

这是什么意思?提前谢谢了!

Ark*_*nez 6

您有一个从属名称,您需要附加typename到const_iterator,因为它的类型取决于类型T.

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

关于依赖名称的C++ faq