QMutex const/this conversion issue



  • I am not sure what it is going on here, I have cleaned my project and I can't get this to go away in one of my classes. I have declared a private variable:

    @
    QMutex _listMutex;
    @

    And then I go to use it:

    @
    QMutexLocker lock(&_listMutex);
    @

    When I compile this one gives me:
    @
    SomeFile.cpp:111: error: C2664: 'QMutexLocker::QMutexLocker(QMutex *)' : cannot convert parameter 1 from 'const QMutex *' to 'QMutex *'
    Conversion loses qualifiers
    @

    And elsewhere:

    @
    _listMutex.lock();
    ...
    _listMutex.unlock();
    @

    and this gives me the compile errors:
    @
    SomeFile.cpp:289: error: C2662: 'QMutex::lock' : cannot convert 'this' pointer from 'const QMutex' to 'QMutex &'
    Conversion loses qualifiers
    SomeFile.cpp:291: error: C2662: 'QMutex::unlock' : cannot convert 'this' pointer from 'const QMutex' to 'QMutex &'
    Conversion loses qualifiers
    @

    Yet I am using this exactly the same way in another class. What is going on?

    Thanks.



  • You are probably trying to lock/unlock the mutex inside a const member function. Since the lock/unlock functions on QMutex are non-const, the compiler complains. Change _listMutex to mutable.

    @
    mutable QMutex _listMutex;
    @



  • Can you pastebin the whole segment? Are you trying to lock a mutex that you have as member from a const method? (If so, use mutable).



  • that was it. Just when one thinks they are getting the hang of const. :)

    Thanks!



  • try declaring the mutex like

    @mutable QMutex _listMutex;@

    I am not sure, just a tought


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.