【发布时间】:2009-11-16 21:57:38
【问题描述】:
创建可与 Qt foreach 宏一起使用的自定义容器的最少代码量是多少?
到目前为止我有这个
template< class T >
class MyList
{
public:
class iterator
{
public:
};
class const_iterator
{
public:
inline iterator& operator++ ()
{
return *this;
}
};
};
我得到这个编译器错误:
4>.\main.cpp(42) : error C2100: illegal indirection
4>.\main.cpp(42) : error C2440: 'initializing' : cannot convert from 'MyList<T>::const_iterator' to 'int'
4> with
4> [
4> T=int
4> ]
4> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
当我尝试编译时:
MyList<int> mylst;
foreach(int num, mylst )
qDebug() << num;
【问题讨论】:
-
请注意,foreach 创建容器的副本,这没什么大不了的,因为它们的类实现了写入时复制,但它可能适合您!
标签: qt foreach containers