<solved> C2662: ‘function’ : cannot convert ‘this’ pointer from ‘type1’ to ‘type2’
Hello people,
I have a QHash<quint32, DataItem> dynamicData which is not const!
- #pragma once
- #include <QtGui>
- #include <QString>
- #include <QHash>
- #include "DataItem.h"
- class DataController
- {
- public:
- DataController(void);
- ~DataController(void);
- int removeDataItem(qint32 key) const;
- protected:
- };
And my source
- #include <QHash>
- #include "DataController.h"
- DataController::DataController(void)
- {
- //
- }
- int DataController::removeDataItem(qint32 key) const
- {
- return this->dynamicData.remove( key ) ;
- }
I get error C2662: ‘QHash<Key,T>::remove’ : cannot convert ‘this’ pointer from ‘const QHash<Key,T>’ to ‘QHash<Key,T> &’
My dynamicData is not constant, so remove should be callable.
Whats the matter? I dont see the error..
Cheers
Huck
5 replies
Your removeDataItem() is const function, and its trying to remove/modify your class member variable. That may be the problem??
It definitely is!
A const method cannot modify the current instance (pointed by “this”). You have a conception problem in here : how could a method that removes an item from a container not modify the instance which “owns” the container…
Of course there are workaround if you REALLY want the method to be const. You can either declare your QHash<qint32, DataItem> mutable or use const_cast to remove the “const” in front of the ‘const QHash<Key,T>’. I would’nt recommend this though.
You must log in to post a reply. Not a member yet? Register here!

