8
創建一個可以與Qt foreach宏一起使用的定製容器的最小代碼量是多少?定製容器要求與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創建容器的副本中找到,這不是一個大問題,因爲他們的類實現寫時複製,但它可能是你! – 2009-11-16 22:40:22