使用 Qt:使用 QList时从 const void* 到 void* 的转换无效;
我已经在 Qt 和 C++ 上摆弄了一段时间了,但是我遇到了这个错误,并且似乎无法弄清楚它为什么会出现。还有很多其他问题已经通过 const void* 转换错误消息得到了解答,但我无法真正看到这些解释对我的情况有什么帮助,所以这里是:
我有一个 QList< 的重新实现“MyTypeManager” ; MyType *const>,因此指向非 const MyType 的 const 指针列表。但是,当我重新实现中的函数调用 addMyType 时,
void MyTypeManager::addMyType(MyType *const var)
{
this->append(var);
}
会发生以下错误:
In file included from /usr/include/qt4/QtCore/QList:1:0,
from ../qtsdlthread/mytypemanager.h:4,
from ../qtsdlthread/mytypemanager.cpp:1:
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:499:13: instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26: instantiated from here
/usr/include/qt4/QtCore/qlist.h:359:58: error: invalid conversion from ‘const void*’ to ‘void*’
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:666:9: instantiated from ‘QList<T>::Node* QList<T>::detach_helper_grow(int, int) [with T = MyType* const]’
/usr/include/qt4/QtCore/qlist.h:497:48: instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26: instantiated from here
/usr/include/qt4/QtCore/qlist.h:386:17: error: invalid conversion from ‘const void*’ to ‘void*’
mytypemanager 中的 20:26 是上面发布的 this->append 行。
I've been messing around with Qt and C++ for a while, but I've run into this error and can't seem to figure out why it crops up. There are a lot of other questions that have been answered out there with the const void* conversion error message, but I can't really see how the explanations help in my case, so here goes:
I have a reimplementation 'MyTypeManager' of QList< MyType *const>, so a list of const pointers to non-const MyTypes. However, when a function in my reimplementation, addMyType is called
void MyTypeManager::addMyType(MyType *const var)
{
this->append(var);
}
the following error(s) occur:
In file included from /usr/include/qt4/QtCore/QList:1:0,
from ../qtsdlthread/mytypemanager.h:4,
from ../qtsdlthread/mytypemanager.cpp:1:
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:499:13: instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26: instantiated from here
/usr/include/qt4/QtCore/qlist.h:359:58: error: invalid conversion from ‘const void*’ to ‘void*’
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:666:9: instantiated from ‘QList<T>::Node* QList<T>::detach_helper_grow(int, int) [with T = MyType* const]’
/usr/include/qt4/QtCore/qlist.h:497:48: instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26: instantiated from here
/usr/include/qt4/QtCore/qlist.h:386:17: error: invalid conversion from ‘const void*’ to ‘void*’
20:26 in mytypemanager is the this->append line posted above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自文档:
可惜
MyType *const
不可分配。您有几种补救措施:1. 使
T
成为可变指针2. 使
T
成为指向 const 指针的指针:但是现在您需要担心 2 级内存管理。
3.(危险)将
T=MyType*
和 const-castMyType *const
转换成MyType *
:这只有在你确定的情况下才有效
var
最初是作为MyType*
变量创建的。From the documentation:
Alas
MyType *const
is not assignable. You have several remedies:1. Make
T
a mutable pointer2. Make
T
a pointer to your const pointer:But now you have 2 levels of memory management to worry about.
3. (Dangerous) Make
T=MyType*
and const-castMyType *const
intoMyType *
:This will only work if you are certain
var
was originally created as aMyType*
variable.