使用qhash->键初始化qlist

时间:2011-08-04 09:13:22

标签: c++ linux qt qt4

执行以下操作时是否会出现错误的初始化:

pdList = new QList<QString>(somehash->keys());

其中,

pdList = QList<QString>*
somehash = QHash<QString,QList<someobject*> > *

此操作在构造函数启动时发生。

询问的原因有时我在做

时遇到了崩溃
pdlist->contains(someqstring)
调用构造函数后的

rigth。 崩溃在

/usr/local/Trolltech/Qt-4.6.3-410wrl/include/QtCore/qlist.h:93

/usr/local/Trolltech/Qt-4.6.3-410wrl/include/QtCore/qlist.h:757

3 个答案:

答案 0 :(得分:0)

您应使用QList< QString >,而不是QList(QString)

答案 1 :(得分:0)

注意下面的模板用法。你应该使用&lt;&gt;字符:

QHash<QString,QList<someObject*> > somehash;
//Populate your hash here

//Populate list with keys
QList<QString> pdList (somehash.keys());

答案 2 :(得分:0)

初步猜测,直到提供其他代码。

也许您依赖于无效的初始化顺序。以下的一些变体:

class MyClass {
public:
  MyClass() :
    somehash(new QHash<QString, QList<MyClass*> >()),
    pdList(new QList<QString>(somehash->keys())) {}
private:
  QList<QString> *pdList;
  QHash<QString, QList<MyClass*> > *somehash;
};

pdList将始终在somehash之前初始化。它是作为数据成员列出的顺序,而不是在构造函数中,它决定了它。有些编译器会让你在构造函数初始化列表中以不正确的顺序放置它们;别人不会。

这只是猜测;如果您提供更多信息,社区将能够更好地提供帮助。

相关问题