2014-08-28 20 views
0

用下面的代码的QList继承导致未被识别为类型类通过的foreach

class Section : public QList<Property> 
{ 
public: 
    explicit Section(); 
    explicit Section(QString name); 
    Property getPropertyNamed(QString name); 
    QString getName(); 
private: 
    QString *m_name; 
}; 


Property Section::getPropertyNamed(QString name) 
{ 
    Property toReturn; 

    foreach (Property loopOn, this) 
    { 
     if(loopOn.getName() == name) 
      toReturn = loopOn; 
    } 

    return toReturn; 
} 

编译器会引发下面的错误。我在源代码中挖掘出了产生错误的原因,但是它坦率地超出了我的理解水平,但它似乎是foreach指令的模板。

error: 'Section* const' is not a class, struct, or union type 
    typename T::const_iterator i, e; 

因此,基本上, - 那里有什么错? - 我该如何解决它?

[标题只是什么是真正发生在这里胡乱猜测,如果有必要,将编辑]

回答

2

的问题是,this是一个指向QList<Property>,但你必须使用类的实例本身,即写:

foreach (Property loopOn, *this) { 
    [..] 
}