jeudi 29 octobre 2015

cannot convert parameter 1 from 'const boost::shared_mutex' to 'const boost::shared_lock

I am just trying to implement this answer about mutexes with over 100 upvotes. All I did was copy paste the code, so that my class reads (simplified) like this:

mymutexclass.h

class MyMutexClass {
public:
    void write( uint32_t id, const std::string &str );
    std::string read( uint32_t id ) const;

private:
    boost::shared_mutex _access;
    std::map<uint32_t, std::string> strings;
};

mymutexclass.cpp

void MyMutexClass::write( uint32_t id, const std::string &str ) {
    boost::unique_lock< boost::shared_mutex > lock(_access);
    strings[id] = name;
}
std::string MyMutexClass::read( const uint32_t id ) const {
    boost::shared_lock< boost::shared_mutex > lock(_access); // ERROR HERE
    if( strings.count( id )>0) 
        return strings.at(id);
    else
        return "No value.";
}

And I get an error, only for the read mutex line:

error C2664: 'boost::shared_lock<Mutex>::shared_lock(const boost::shared_lock<Mutex> &)' : cannot convert parameter 1 from 'const boost::shared_mutex' to 'const boost::shared_lock<Mutex> &'   D:\... path ...\mymutexclass.cpp

I am completely lost in the error - what's the difference between the types it complains about? I mean these, from the error:

  • from const boost::shared_mutex
  • to const boost::shared_lock<Mutex> &

And what am I doing wrong? That linked answer was upvoted 100 times, so I guess it's really likely to be correct.

Aucun commentaire:

Enregistrer un commentaire