试图创建一个类来跟踪最近 2 秒内的数据包,但出现编译错误。有任何想法吗?谢谢。
g++ -std=c++11 map2.cc
map2.cc:20:2: error: invalid use of template-name ‘std::iterator’ without an argument list
iterator begin() { return l.begin(); }
这是小类:
#include <iostream>
#include <string>
#include <list>
#include <unordered_map>
using namespace std;
class cEvents {
public:
list<pair<double, int>> l;
int add(double ts, int pktNum, double maxTime) {
l.push_back(make_pair(ts, pktNum));
pair<double, int> tmp;
while (1) {
tmp = l.front();
if ((ts - tmp.first) < maxTime) break;
l.pop_front();
}
return l.size();
}
iterator begin() { return l.begin(); }
};
int main () {
cEvents e;
cout << e.add(0, 1, 2) << endl;
cout << e.add(0.1,2, 2) << endl;
cout << e.add(0.2,3, 2) << endl;
cout << e.add(0.5,4, 2) << endl;
cout << e.add(1.2,5, 2) << endl;
cout << e.add(1.7,6, 2) << endl;
for (auto x : e) {
//cout << x.first << " " << x.second << endl;
}
cout << e.add(2.2,7, 2) << endl;
cout << e.add(3.2,8, 2) << endl;
return 0;
}
最佳答案
需要使用容器的迭代器作为返回类型。
iterator begin() { return l.begin(); }
应该是
list<pair<double, int>>::iterator begin() { return l.begin(); }
或者在 C++14 中
auto begin() { return l.begin(); }
关于c++ - 类的 begin() 方法编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34183401/