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

Bob*_*phy 5 c++ inheritance templates stl

在前一段时间处理一些图形代码时,我使用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;
    }
    ...
};
Run Code Online (Sandbox Code Playgroud)

当该代码不是模板的一部分,因此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
Run Code Online (Sandbox Code Playgroud)

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

std::list< KRect<T> >::iterator i;
this->iterator i;
Run Code Online (Sandbox Code Playgroud)

但似乎没什么用.

有什么建议?

Geo*_*che 9

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

typename std::list< KRect<T> >::iterator i;
Run Code Online (Sandbox Code Playgroud)

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

template <typename T>
class KRegion : public std::list< KRect<T> >
{
    typedef std::list< KRect<T> > base;
    typedef typename base::iterator iterator;
    // ...
};
Run Code Online (Sandbox Code Playgroud)