关于模板的一个问题
写了段小程序老是报错,大家帮忙看下
#include <map>
#include <string>
using namespace std;
template <class T>
class test
{
public:
void add(const char* index, T* data);
void remove(const char* index);
int getSize();
private:
map<string, T*> m_map;
};
template <class T>
int test<T>::getSize()
{
return m_map.size();
}
template <class T>
void test<T>::add(const char* index, T* data)
{
m_map[index] = data;
}
template <class T>
void test<T>::remove(const char* index)
{
map<string, T*>::iterator it = m_map.find(index);
if ( it != m_map.end() )
{
delete (T*)(it->second);
m_map.erase(it);
}
}
int main()
{
test<int> a;
return 0;
}
编译错误:
g++ -o a template.cpp
template.cpp: In member function ‘void test<T>::remove(const char*)’:
template.cpp:33: error: expected `;' before ‘it’
template.cpp:35: error: ‘it’ was not declared in this scop