[英]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
限定符,因为使用cv
或ref限定符限定非成员函数是非法的,甚至没有任何意义。 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.