gpt4 book ai didi

c++ - 这个静态模板函数的实现有什么问题?

转载 作者:搜寻专家 更新时间:2023-10-31 01:13:31 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

我正在学习模板函数。我正在尝试实现一个清除指针列表的静态模板函数。为此,我想使用模板。这是我的代码:

#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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com