繁体   English   中英

迭代器类型应该在这个C ++模板中应该是什么?

[英]What should the iterator type be in this C++ template?

在前一段时间处理一些图形代码时,我使用int作为底层坐标持有者编写了Rect和Region类,并且工作正常。 Region实现为STL列表的简单类扩展,只包含一个Rects列表。

现在我还需要使用双精度作为底层坐标持有者的同类课程,并决定尝试将其模板化。 所以我基本上以“智能方式”将“int”替换为“typename T”并修复了问题。

但是还有一个问题让我难过。 我想通过在构成它的所有Rect上进行并集来计算Region的边界框。 在没有模板化的情况下工作正常,但是当它被模板化时,g ++会在列表迭代器上产生扼流圈。

这是相关的代码:

// Rect class that always remains normalized
template <typename T>
class KRect
{
public:

    // Ctors
    KRect(void)
        : _l(0), _t(0), _r(0), _b(0)
    {
    }
    void unionRect(const KRect& r)
    {
        ...
    }

private:
    T _l, _t, _r, _b;
};

// Region class - this is very brain-dead
template <typename T>
class KRegion : public std::list< KRect<T> >
{
public:
    ...

    // Accessors
    KRect<T> boundingBox(void)
    {
        KRect<T> r;
        iterator i;
        for (i = this->begin(); i != this->end(); i++)
        {
            r.unionRect(*i);
        }
        return r;
    }
    ...
};

当该代码不是模板的一部分,因此T是明确的(例如int)时,“iterator i”行工作正常。 但是你在上面看到的,Ubuntu上的g ++会发出错误,我发现这些错误信息非常丰富:

include/KGraphicsUtils.h: In member function ‘KRect<T> KRegion<T>::boundingBox()’:
include/KGraphicsUtils.h:196: error: expected ‘;’ before ‘i’
include/KGraphicsUtils.h:197: error: ‘i’ was not declared in this scope
include/KGraphicsUtils.h: In member function ‘KRect<T> KRegion<T>::boundingBox() [with T = int]’:
--- redacted ---:111:   instantiated from here
include/KGraphicsUtils.h:196: error: dependent-name ‘std::foo::iterator’ is parsed as a non-type, but instantiation yields a type
include/KGraphicsUtils.h:196: note: say ‘typename std::foo::iterator’ if a type is meant

我猜这是一个类型资格问题,有一些我不熟悉的模板-y旋转。 我尝试过各种各样的事情:

std::list< KRect<T> >::iterator i;
this->iterator i;

但似乎没什么用。

有什么建议?

iterator是一个依赖类型(它取决于模板参数),需要以typename为前缀:

typename std::list< KRect<T> >::iterator i;

更好的风格是提供类范围的typedef:

template <typename T>
class KRegion : public std::list< KRect<T> >
{
    typedef std::list< KRect<T> > base;
    typedef typename base::iterator iterator;
    // ...
};

我认为gf有你的答案 ,但我想建议让区域管理列表作为成员而不是基类:

template <typename T>
class KRegion
{
protected:
     typedef std::list< KRect<T> > ListType;
     ListType list;
public:
    ...
    // Accessors
    void addRect(KRect<T> & rect) { list->push_back(rect); }
    ...
    KRect<T> boundingBox(void)
    {
        KRect<T> r;
        ListType::iterator i;
        for (i = list->begin(); i != list->end(); i++)
        {
            r.unionRect(*i);
        }
        return r;
    }
    ...
};

我对此建议的动机是,有一天,您可能希望使用不同的容器来存储您的KRect,并且将列表作为内部成员可以让您这样做而不会破坏所有客户端代码。

暂无
暂无

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

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