迭代器类型应该在这个C ++模板中应该是什么? [英] What should the iterator type be in this C++ template?

查看:173
本文介绍了迭代器类型应该在这个C ++模板中应该是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While working on some graphics code a while back, I wrote Rect and Region classes using ints as the underlying coordinate holder, and that worked fine. The Region was implemented as a simple class extension to an STL list, and just contains a list of Rects.

现在我还需要使用双精度的同类课程潜在的坐标持有者,并决定尝试将其模板化。因此我基本上以聪明的方式将int替换为typename T并修复了问题。

Now I also need the same kinds of classes using doubles as the underlying coordinate holder, and decided to try my hand at templatizing it. So I basically replaced "int" with "typename T" in an intelligent manner and fixed the problems.

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

But there's one remaining problem that has me stumped. I want to calculate a Region's bounding box by doing a union on all the Rects that comprise it. That works fine when not templatized, but g++ chokes on the list iterator when this is templatized.

以下是相关代码:

// 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) ,迭代器我线工作正常。但是你在上面看到的,Ubuntu上的g ++会发出错误,我发现这些错误信息不多:

When that code isn't part of a template, so that T is definite (e.g. an int), the "iterator i" line works fine. But in what you see above, g++ on Ubuntu emits errors which I don't find very informative:

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旋转我是不熟悉。我尝试过各种各样的事情:

My guess is this is a type qualification issue with some template-y spin I'm not familiar with. I've tried all kinds of things like:

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

但似乎没有任何效果。

有什么建议吗?

推荐答案

iterator 是一种依赖类型(取决于模板参数)并且需要以 typename 作为前缀:

iterator is a dependent type (it depends on a template argument) and needs to be prefixed with typename:

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

更好的风格是提供全班级的typedef:

Better style would be to provide a class-wide typedef:

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

这篇关于迭代器类型应该在这个C ++模板中应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆