2010-05-20 29 views
0

为什么我不能编译这段代码?简单图<>实例化,我无法编译,请帮忙

#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(); 
} 

我的编译器说:

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

它是什么?提前谢谢了!

回答

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(); 
} 

C++ faq on depenent names

相关问题