此 C++ 中的迭代器类型应该是什么?模板?
前一段时间在处理一些图形代码时,我使用整数作为底层坐标持有者编写了 Rect 和 Region 类,并且效果很好。 Region 被实现为 STL 列表的简单类扩展,并且仅包含 矩形列表。
现在我还需要使用双精度数作为底层坐标持有者的相同类型的类,并决定尝试将其模板化。所以我基本上以智能的方式用“typename T”替换了“int”并修复了问题。
但还有一个问题困扰着我。我想通过对组成区域的所有矩形进行并集来计算区域的边界框。当没有模板化时,它工作得很好,但是当它被模板化时,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
我的猜测是,这是一个类型限定问题,其中包含一些我不熟悉的 template-y spin。我尝试过各种方法,例如:
std::list< KRect<T> >::iterator i;
this->iterator i;
但似乎没有任何效果。
有什么建议吗?
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.
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.
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.
Here's the relevant code:
// 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;
}
...
};
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
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;
but nothing seems to work.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
iterator
是一个依赖类型(它依赖于模板参数),需要以typename
为前缀:更好的风格是提供类范围的 typedef:
iterator
is a dependent type (it depends on a template argument) and needs to be prefixed withtypename
:Better style would be to provide a class-wide typedef:
我认为gf有你的答案,但我想建议让区域作为成员而不是基类来管理列表:
我提出此建议的动机是,有一天,您可能想要使用不同的容器来存储您的 KRect,并且将该列表作为内部成员可以让您在不破坏所有客户端代码的情况下执行此操作。
I think gf has your answer, but I'd like to suggest having the region manage a list as a member instead of a base class:
My motivation for this suggestion is that you may, one day, want to use a different container for storing your KRects, and having the list as an internal member would let you do so without breaking all of your client code.