C++ 类的begin()方法上的编译错误

C++ 类的begin()方法上的编译错误,c++,C++,试图创建一个类来跟踪最近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>

试图创建一个类来跟踪最近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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类事件{
公众:
清单l;
int add(双ts、int pktNum、双maxTime){
l、 向后推(形成配对(ts、pktNum));
成对tmp;
而(1){
tmp=左前();
如果((ts-tmp.first)无法将返回类型与要返回的内容匹配

尝试以下操作:
list::iterator begin(){return l.begin();}

更新:编译器告诉我必须创建
end()
函数作为
类cEvents
的成员

list<pair<double, int>>::iterator end() { return l.end(); }
list::迭代器end(){return l.end();}

您需要使用容器的迭代器作为返回类型

iterator begin() { return l.begin(); }
应该是

list<pair<double, int>>::iterator begin() { return l.begin(); }
您已经定义了
begin()
以返回名为
iterator
的某种类型,但还没有定义实际的类型

编译器可以通过该名称找到的唯一类型是
std::iterator
,它是一个类模板,因此如果没有模板参数列表,就不能使用它(在本例中,它可能不是您想要的)

在这种情况下,在我看来,您可能希望
begin()
(和
end()
)将迭代器返回到对象包含的
列表中,因此您可以执行以下操作:

typedef list<pair<double, int>>::iterator iterator;
…或同等(但至少在某些人看来,更容易理解):

然后,既然您已经定义了迭代器的含义,那么您可以使用该名称作为函数的返回类型(或者作为参数等),因为您显然有一些类似于集合的功能,您最好也为集合定义其他“标准”类型(例如,
typedef std::pair value\u type;

我建议不要直接将
std::list::iterator
指定为返回类型。这确实有效,但它包含大量冗余。您总是希望该返回类型与集合的迭代器类型相同,因此如果您改变主意,使用其他集合而不是
list
,则函数需要更改以匹配。使用
decltype(l)::iterator
会自动进行更改,而使用
std::list::iterator
则需要手动编辑以保持类型同步


我应该补充一点,在这种特殊情况下,我认为尽可能容易地更改为不同的类型比通常更重要。具体地说,使用
std::list
是一个错误,因此除非这是一次性代码,否则想要更改为不同容器的可能性非常高

谢谢你的快速回答。它很有效!因此阻止我现在接受它。我今天晚些时候会这样做。它有效,但是(IMO)解决问题的方法仍然是错误的。方便的解决方案:更改begin()的返回类型自动
使用名称空间std;
会造成这里的混乱;这就是为什么编译器认为
迭代器
指的是
std::迭代器
。这就是为什么
使用名称空间std;
是个坏主意。@metalmeshiah,在这里有
自动
会很好,但我得到了警告
map2.cc:20:13:warning:'begin'函数使用“auto”类型说明符,不带尾部返回类型[默认启用]auto begin(){return l.begin();}
当我将其更改为
auto begin(){return l.begin();}
时,感谢@pete becker的警告!感谢@NathanOliver,您的解决方案也很好。我喜欢c++14,但它在我的g++(4.8.4)上不可用不过。谢谢@jerry coffin提供的详细答案。非常喜欢你建议使用
decltype(l)::迭代器,它看起来更简单、更健壮。
typedef list<pair<double, int>>::iterator iterator;
typedef decltype(l)::iterator iterator;
using iterator = decltype(l)::iterator;