[英]How to create a QSharedPointer<QMap<a,b>> with an initializer list?
In Qt 5.12.4, I try to dynamically create QSharedPointer<QMap<int,bool>>
.在 Qt 5.12.4 中,我尝试动态创建QSharedPointer<QMap<int,bool>>
。 I know that I can do我知道我能做到
QMap<int, bool> mp = { { 1, true } };
as well as dynamically with以及动态地
QMap<int, bool>* mpptr = new QMap<int, bool> { { 1, false } };
I know that I can create my QSharedPointer
like so:我知道我可以像这样创建我的QSharedPointer
:
QMap<int, bool>* mpptr = new QMap<int, bool> { { 1, false } };
QSharedPointer<QMap<int, bool>> shraredmpptr(mpptr);
I want do this in one single shot, which I am not able to accomplish.我想一口气做到这一点,但我无法完成。 I tried the most obvious我试过最明显的
QSharedPointer<QMap<int, bool>> mpsptr =
QSharedPointer<QMap<int, bool>>::create(new QMap<int, bool> { { 1, false } });
but to no avail.但无济于事。 The compiler gives me warnings that my types do not match but I do not see the point where I have to change the code so that it compiles.编译器警告我我的类型不匹配,但我没有看到我必须更改代码才能编译的地方。 Any Ideas?有任何想法吗? Thanks in advance.提前致谢。
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
The arguments passed to create are durectly passed to the constructor.传递给创建的 arguments 直接传递给构造函数。 Therefore, you can just do:因此,您可以这样做:
QSharedPointer<QMap<int, bool>> mpsptr = QSharedPointer<QMap<int, bool>>::create({
{ 1, false }
});
And if you use C++11 or above, why not use auto?如果您使用 C++11 或更高版本,为什么不使用 auto?
auto mpsptr = QSharedPointer<QMap<int, bool>>::create({
{ 1, false }
});
This worked for me (compiled with GCC, C++ x86 64bit ):这对我有用(用GCC, C++ x86 64bit编译):
QSharedPointer<QMap<int, bool>> mpsptr = QSharedPointer<QMap<int, bool>>::create(QMap<int, bool>{{1, false}});
Ok, I found an answer that works for me.好的,我找到了一个适合我的答案。 the above one did not compile with msvc17 and with my arm toolchain.上面的没有用 msvc17 和我的 arm 工具链编译。 Also my first approach produces a memory leak.我的第一种方法也会产生 memory 泄漏。
The correct way to do is is正确的做法是
QSharedPointer<QMap<int, bool>> mpsptr(new QMap<int, bool> { { 1, false } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.