如何使用初始化列表创建QSharedPointer <QMap <a,b >>?

时间:2019-10-25 08:31:41

标签: qt c++11 qmap qsharedpointer

什么有效

在Qt 5.12.4中,我尝试动态创建QSharedPointer<QMap<int,bool>>。我知道我能做到

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

2 个答案:

答案 0 :(得分: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 }
});

答案 1 :(得分:-2)

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

正确的方法是

array(
  [0] = array(
    [0] => e348ae92
    [1] => custommers
    [2] => 614
  )
  [1] = array(
    [0] => 193ba701
    [1] => Suppliers
    [2] => 65
  )
  [2] = array (
    [0] => 58695854
    [1] => Users
    [2] => 99
  )
)