为什么这段代码在VS13中成功编译并且无法通过gcc编译?
template <typename T>
class Base
{
protected:
typedef std::map<T, double> MyMap;
MyMap m_map;
public:
void func(const T& key)
{
typename MyMap::iterator it = m_map.find(key);
if(it != m_map.end()) {
// ....
}
}
};
class Inherited1 : public Base <char>
{ };
class Inherited2 : public Base <int>
{ };
导致以下错误(gcc 4.1.2)
test.cpp: In member function ‘void Base<T>::func(const T&)’:
test.cpp:16: error: expected `;' before ‘it’
test.cpp:17: error: ‘it’ was not declared in this scope
EDIT1:
谢谢你的回复。尝试使用关键字typename
作为juanchopanza建议,现在我得到了这个错误:
/tmp/ccotJWFl.o: In function `__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, double> > >::deallocate(std::_Rb_tree_node<std::pair<int const, double> >*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKidEEE10deallocateEPS5_m[__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, double> > >::deallocate(std::_Rb_tree_node<std::pair<int const, double> >*, unsigned long)]+0x19): undefined reference to `operator delete(void*)'
/tmp/ccotJWFl.o: In function `__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<char const, double> > >::deallocate(std::_Rb_tree_node<std::pair<char const, double> >*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKcdEEE10deallocateEPS5_m[__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<char const, double> > >::deallocate(std::_Rb_tree_node<std::pair<char const, double> >*, unsigned long)]+0x19): undefined reference to `operator delete(void*)'
/tmp/ccotJWFl.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
EDIT2:
这整个事情变得更加荒谬 - 如果我写gcc test.cpp
g++ test.cpp
编译,就会导致上述错误