我正在学习模板功能。我正在尝试实现一个清除指针列表的静态模板函数。为此,我想使用模板。它是我的代码:
#include <cstdio>
#include <list>
using namespace std;
class util
{
public:
template <class ARG>
static void CleanPointers(list<ARG> &mylist) {
list<ARG>::iterator it;
for (it = mylist.begin(); it != mylist.end(); it++)
{
ARG obj = (ARG) *it;
delete obj;
}
mylist.clear();
};
util();
~util();
};
int main()
{
list<int*> mylist;
mylist.push_back(new int(1));
mylist.push_back(new int(2));
util::CleanPointers<int*>(mylist);
return 0;
}
我收到了以下编译错误消息,但我不明白这里有什么意义。:) 为什么需要我放;之前呢?
prog.cpp: In static member function ‘static void util::CleanPointers(std::list<ARG, std::allocator<_Tp1> >&)’:
prog.cpp:10: error: expected `;' before ‘it’
prog.cpp:11: error: ‘it’ was not declared in this scope
prog.cpp: In static member function ‘static void util::CleanPointers(std::list<ARG, std::allocator<_Tp1> >&) [with ARG = int*]’:
prog.cpp:28: instantiated from here
prog.cpp:10: error: dependent-name ‘std::list::iterator’ is parsed as a non-type, but instantiation yields a type
prog.cpp:10: note: say ‘typename std::list::iterator’ if a type is meant