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.
From the documentation:
QList's value type must be an assignable data type.
Alas MyType *const
is not assignable. You have several remedies:
T
a mutable pointerT
a pointer to your const pointer:typedef MyType *const Element
void MyTypeManager::addMyType(Element var)
{
Element* ptr2ptr = new Element(var);
this->append(ptr2ptr);
}
But now you have 2 levels of memory management to worry about.
T=MyType*
and const-cast MyType *const
into MyType *
:this->append(const_cast<MyType *>(var));
This will only work if you are certain var
was originally created as a MyType*
variable.