【发布时间】:2011-09-02 15:03:16
【问题描述】:
我一直在搞乱 Qt 和 C++,但我遇到了这个错误,似乎无法弄清楚它为什么会突然出现。 const void* 转换错误消息已经回答了很多其他问题,但我真的看不出这些解释对我的情况有什么帮助,所以这里是:
我有一个 QList
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 行。
【问题讨论】:
-
必须使用 const 指针有充分的理由吗?
-
我认为将它们设为 const 是个好主意,因为它们不应该被更改,只是为了 const 的正确性。
-
如果是对象本身不应该被改变,那么你应该使用
MyType const *而不是MyType * const。QtList<MyType const *>应该可以工作。
标签: c++ qt pointers constants qlist