简体   繁体   English

从函数返回迭代器给我一个奇怪的错误

[英]Returning an iterator from a function gives me a weird error

So I have my class named Client and it contains a list of strings. 所以我有一个名为Client的类,它包含一个字符串列表。 I want a method to get an iterator at the beginning and another at the end. 我想要一种在开始时获得迭代器而在结束时获得迭代器的方法。 So I have the following code: 所以我有以下代码:

iterator Client::getGustBegin() const { return _gustos.begin(); }
iterator Client::getGustEnd() const { return _gustos.end(); }

And in the Client.h it's like this: 在Client.h中,它是这样的:

    iterator getGustBegin() const;
    iterator getGustEnd() const;

Also _gustos is declared like this: _gustos也这样声明:

list<string> _gustos;

I'm using g++ compiler and it shows me this error: 我正在使用g ++编译器,它向我显示此错误:

error: invalid use of template-name 'std::iterator' without an argument list

In both of the lines in the Client.h. 在Client.h的两行中。 I have the map and list includes. 我有地图和清单,包括在内。 I don't know why and I don't understand the error. 我不知道为什么,我也不明白错误。 What am I doing wrong? 我究竟做错了什么?

A typedef is often a good thing: typedef通常是一件好事:

class Client
{
   public:
   typedef std::list<std::string>::iterator gust_iterator;
   gust_iterator getGustBegin() const;
};


inline Client::gust_iterator Client::getGustBegin() const {
   return _gustos.begin();
}

In your case (as already pointed out) iterator is referring to std::iterator. 在您的情况下(如已指出的那样),迭代器是指std :: iterator。 (Hence, you gave another example for avoiding 'using nmaespace std') (因此,您给出了避免“使用nmaespace std”的另一个示例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM