简体   繁体   English

错误:没有匹配函数调用&#39;boost :: shared_lock <boost::shared_mutex> :: shared_lock(const Lock&)&#39;

[英]error: no matching function for call to 'boost::shared_lock<boost::shared_mutex>::shared_lock(const Lock&)'

I have implemented a ReadLock like following: 我已经实现了如下所示的ReadLock:

In my myClass.h 在我的myClass.h中

#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>

typedef boost::shared_mutex Lock;
typedef boost::shared_lock< Lock > ReadLock;

Lock myLock;

In myClass.cpp: 在myClass.cpp中:

void ReadFunction() const
{
    ReadLock r_lock(myLock); // Error!
    //Do reader stuff
}

The code works in VS2010 but failed with GCC4.0. 该代码在VS2010中有效,但在GCC4.0中失败。 The compiler is throwing error at ReadLock saying there is no matching function. 编译器在ReadLock上抛出错误,表明没有匹配的函数。 I suspect is the "const" correctness problem with the variable myLock. 我怀疑变量myLock是“ const”正确性问题。 When I removed the const in the function declaration, the error disappeared. 当我在函数声明中删除const时,错误消失了。 Can anybody explain this to me? 有人可以向我解释吗? Why this works under windows but not with gcc? 为什么这可以在Windows下工作,但不能在gcc下工作?

Any suggestion here? 有什么建议吗? Thanks. 谢谢。

You should either remove the const qualifier from ReadFunction() , because qualifying a non-member function with cv or ref qualifiers is illegal and doesn't even makes sense; 您应该从ReadFunction()删除const限定符,因为使用cvref限定符限定非成员函数是非法的,甚至没有任何意义。 or you encapsulate whatever you are trying to do in a class . 或者将您要尝试做的事情封装在class


void ReadFunction() const
{
    ReadLock r_lock(myLock); // Error!
    //Do reader stuff
}

const can only be applied to member functions. const只能应用于成员函数。 The above code isn't a member function, if it was, it would be, (for example, a class named MyClass ): 上面的代码不是成员函数,如果是,则为成员函数(例如,名为MyClass的类):

void MyClass::ReadFunction() const
{
    ReadLock r_lock(myLock);
    //Do reader stuff
}

And in that case, you would typically need to make lock a mutable member. 在这种情况下,您通常需要将lock设为mutable成员。 by declaring it like this: 通过这样声明:

class MyClass{
    ....
    mutable Lock myLock;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将参数1从&#39;const boost :: shared_mutex&#39;转换为&#39;const boost :: shared_lock <Mutex> &” - cannot convert parameter 1 from 'const boost::shared_mutex' to 'const boost::shared_lock<Mutex> &' 将boost :: shared_lock升级为独占锁 - Upgrading boost::shared_lock to exclusive lock 错误:使用ReaderLock = std :: shared_lock的&#39;shared_mutex&#39;不是&#39;std&#39;的成员 <std::shared_mutex> ; - error: ‘shared_mutex’ is not a member of ‘std’ using ReaderLock = std::shared_lock<std::shared_mutex>; Boost Shared_lock / Unique_lock,给作者优先权? - Boost Shared_lock / Unique_lock, giving the writer priority? 带有std :: shared_lock的std :: shared_mutex是读者还是作家更喜欢? - std::shared_mutex with std::shared_lock is reader or writer preferring? 我可以在 std::shared_mutex 上使用 std::shared_lock 更改数据吗? - Can I change data with a std::shared_lock on a std::shared_mutex? std :: lock()是否等同于boost :: shared_mutex? - std::lock() equivalent for boost::shared_mutex? 读写器锁的boost :: unique_lock和boost :: shared_lock - boost::unique_lock and boost::shared_lock for reader writer locks 使用boost :: upgrade_lock和shared_lock实现读/写线程安全 - Using boost::upgrade_lock and shared_lock to implement read/write thread safety 用 std::atomic 实现的 shared_lock - shared_lock implemented with std::atomic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM