【问题标题】:How to create a QSharedPointer<QMap<a,b>> with an initializer list?如何使用初始化列表创建 QSharedPointer<QMap<a,b>>?
【发布时间】:2020-02-21 14:14:09
【问题描述】:

什么有效

在 Qt 5.12.4 中,我尝试动态创建 QSharedPointer&lt;QMap&lt;int,bool&gt;&gt;。我知道我能做到

QMap<int, bool> mp = { { 1, true } };

以及动态使用

QMap<int, bool>* mpptr = new QMap<int, bool> { { 1, false } };

我知道我可以像这样创建我的QSharedPointer

QMap<int, bool>* mpptr = new QMap<int, bool> { { 1, false } };
        QSharedPointer<QMap<int, bool>> shraredmpptr(mpptr);

什么没有

我想一次性完成,但我无法完成。我尝试了最明显的

QSharedPointer<QMap<int, bool>> mpsptr =
            QSharedPointer<QMap<int, bool>>::create(new QMap<int, bool> { { 1, false } });

但无济于事。编译器警告我我的类型不匹配,但我没有看到我必须更改代码以便编译的地方。有任何想法吗?提前致谢。

编译器错误

In file included from D:\Qt\5.12.4\mingw73_64\include/QtCore/qsharedpointer.h:48:0,
                 from D:\Qt\5.12.4\mingw73_64\include/QtCore/qdebug.h:54,
                 from D:\Qt\5.12.4\mingw73_64\include\QtCore/qloggingcategory.h:44,
                 from D:\Qt\5.12.4\mingw73_64\include\QtCore/QLoggingCategory:1,
                 from ..\..\..\..\myLibsQt\src\libs/a/foo.h:4,
                 from ..\..\..\..\myLibsQt\src\libs/a/bar.h:8,
                 from ..\..\..\..\myLibsQt\src\libs/b/baz.h:8,
                 from ..\..\..\..\myLibsQt\src\libs/c/bla.h:8,
                 from ..\..\..\..\myLibsQt\src\libs\c\blubb.cpp:1:
D:\Qt\5.12.4\mingw73_64\include/QtCore/qsharedpointer_impl.h: In instantiation of 'static QSharedPointer<T> QSharedPointer<T>::create(Args&& ...) [with Args = {QMap<int, bool>*}; T = QMap<int, bool>]':
..\..\..\..\myLibsQt\src\libs\c\blubb.cpp:27:80:   required from here
D:\Qt\5.12.4\mingw73_64\include/QtCore/qsharedpointer_impl.h:445:9: error: no matching function for call to 'QMap<int, bool>::QMap(QMap<int, bool>*)'
         new (ptr) T(std::forward<Args>(arguments)...);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from D:\Qt\5.12.4\mingw73_64\include/QtCore/qdebug.h:47:0,
                 from D:\Qt\5.12.4\mingw73_64\include\QtCore/qloggingcategory.h:44,
                 from D:\Qt\5.12.4\mingw73_64\include\QtCore/QLoggingCategory:1,
                 from ..\..\..\..\myLibsQt\src\libs/a/foo.h:4,
                 from ..\..\..\..\myLibsQt\src\libs/a/bar.h:8,
                 from ..\..\..\..\myLibsQt\src\libs/b/baz.h:8,
                 from ..\..\..\..\myLibsQt\src\libs/c/bla.h:8,
                 from ..\..\..\..\myLibsQt\src\libs\c\blubb.cpp:1:
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:1164:22: note: candidate: QMap<K, V>::QMap(const std::map<Key, T>&) [with Key = int; T = bool]
 Q_OUTOFLINE_TEMPLATE QMap<Key, T>::QMap(const std::map<Key, T> &other)
                      ^~~~~~~~~~~~
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:1164:22: note:   no known conversion for argument 1 from 'QMap<int, bool>*' to 'const std::map<int, bool, std::less<int>, std::allocator<std::pair<const int, bool> > >&'
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:343:12: note: candidate: QMap<K, V>::QMap(QMap<K, V>&&) [with Key = int; T = bool]
     inline QMap(QMap<Key, T> &&other) Q_DECL_NOTHROW
            ^~~~
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:343:12: note:   no known conversion for argument 1 from 'QMap<int, bool>*' to 'QMap<int, bool>&&'
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:622:8: note: candidate: QMap<K, V>::QMap(const QMap<K, V>&) [with Key = int; T = bool]
 inline QMap<Key, T>::QMap(const QMap<Key, T> &other)
        ^~~~~~~~~~~~
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:622:8: note:   no known conversion for argument 1 from 'QMap<int, bool>*' to 'const QMap<int, bool>&'
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:330:12: note: candidate: QMap<K, V>::QMap(std::initializer_list<std::pair<_T1, _T2> >) [with Key = int; T = bool]
     inline QMap(std::initializer_list<std::pair<Key,T> > list)
            ^~~~
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:330:12: note:   no known conversion for argument 1 from 'QMap<int, bool>*' to 'std::initializer_list<std::pair<int, bool> >'
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:328:12: note: candidate: QMap<K, V>::QMap() [with Key = int; T = bool]
     inline QMap() Q_DECL_NOTHROW : d(static_cast<QMapData<Key, T> *>(const_cast<QMapDataBase *>(&QMapDataBase::shared_null))) { }
            ^~~~
D:\Qt\5.12.4\mingw73_64\include/QtCore/qmap.h:328:12: note:   candidate expects 0 arguments, 1 provided
mingw32-make[1]: *** [Makefile.Debug:1488: debug/blubb.o] Error 1

【问题讨论】:

  • 从编译器错误:QSharedPointer&lt;T&gt;::create(Args&amp;&amp; ...) 你可以从中读到你应该传递多个参数 - 而不仅仅是一个指针。这是第一个提示。同样来自:no mying function for call to 'QMap&lt;int, bool&gt;::QMap(QMap&lt;int, bool&gt;*)'(原文如此——“mying”是怎么回事?)。从中可以看出,Qt 试图将您的指针传递给QMap 的构造函数。这些提示应该是向前迈出的一大步。
  • 错字。读取no matching。复制粘贴错误。我找到了解决方案。那个地方的创造是错误的。它从引用中创建一个新的。它通过常规构造函数工作。

标签: qt c++11 qmap qsharedpointer


【解决方案1】:

传递给 create 的参数直接传递给构造函数。因此,您可以这样做:

QSharedPointer<QMap<int, bool>> mpsptr =  QSharedPointer<QMap<int, bool>>::create({
  { 1, false }
});

如果你使用 C++11 或更高版本,为什么不使用 auto 呢?

auto mpsptr =  QSharedPointer<QMap<int, bool>>::create({
  { 1, false }
});

【讨论】:

  • 似乎无法用 msvc 15.x 编译
  • 您的两个构造都不能使用 msvc17 和我使用的 arm 工具链进行编译。我在自己的答案中使用的那个。
【解决方案2】:

这对我有用(使用 GCC、C++ x86 64 位编译):

QSharedPointer<QMap<int, bool>> mpsptr = QSharedPointer<QMap<int, bool>>::create(QMap<int, bool>{{1, false}});

【讨论】:

    【解决方案3】:

    好的,我找到了适合我的答案。上面的没有用 msvc17 和我的 arm 工具链编译。我的第一种方法也会产生内存泄漏。

    正确的做法是

    QSharedPointer<QMap<int, bool>> mpsptr(new QMap<int, bool> { { 1, false } });
    

    【讨论】:

    • 这绝对不是你应该如何使用QSharedPointer&lt;&gt;::create()
    • 因为即使修改也是不准确的:就像你说的那样,它会造成内存泄漏,而共享指针应该有助于自动内存管理。
    • 我修改了我的答案,因此您的评论对其他人不再有用,可能您考虑将其删除。
    猜你喜欢
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多