使用 qhash->keys 初始化 qlist

发布于 2024-11-27 20:53:31 字数 519 浏览 1 评论 0原文

在执行以下操作时是否会发生错误的初始化:

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

其中,

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

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

询问的原因是有时我

pdlist->contains(someqstring)

在调用构造函数后执行正确操作时会发生崩溃。 崩溃位于

/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

Can there occur a bad initialization while doing the following things:

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

where,

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

this operation happens at startup in constructor.

The reason for asking is sometimes i get a crash while doing

pdlist->contains(someqstring)

rigth after the constructor is called.
The crash is in

/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒尘 2024-12-04 20:53:31

注意模板的使用,如下所示。您应该使用 <>人物:

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

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

Pay attention to template usage like below . You should use <> characters:

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

//Populate list with keys
QList<QString> pdList (somehash.keys());
囍笑 2024-12-04 20:53:31

您应该使用 QListQList< QString >,与 QList(QString) 相反。

You should use QList< QString >, as opposed to QList(QString).

半山落雨半山空 2024-12-04 20:53:31

在提供额外代码之前进行初步猜测。

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

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 之前初始化。决定它的是它们作为数据成员列出的顺序,而不是在构造函数中。有些编译器会让你将它们以不正确的顺序放入构造函数初始化列表中;其他人则不会。

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

Preliminary guess until additional code is provided.

Perhaps you are depending on the order of initialization that is invalid. Some variant of the following:

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 will always be initialized before somehash. It is the order they are listed as data members, not in the constructor, that determines it. Some compilers will let you put them in the incorrect order in the constructor initialization list; others will not.

This is just a guess; if you provide more information, the community will be better able to help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文