编译错误:在此上下文中无法隐式捕获'this'
Compilation error : 'this' cannot be implicitly captured in this context
我正试图添加一个condition_variable
来处理线程,但在这一行遇到了编译错误:
this->cv.wait(lk, []{return this->ready;});
对于变量this->ready
,this
似乎不在正确的范围内。
在Java中,这可以用TestThread.this
来处理,C++中有什么可以做同样的事情吗?
void TestThread::Thread_Activity()
{
std::cout << "Thread started n";
// Wait until ThreadA() sends data
{
std::unique_lock<std::mutex> lk(m);
this->cv.wait(lk, []{return ready;});
}
std::cout << "Thread is processing datan";
data += " after processing";
// Send data back to ThreadA through the condition variable
{
// std::lock_guard<std::mutex> lk(m);
processed = true;
// std::cout << "Thread B signals data processing completedn";
}
}
您需要捕获this
指针:
this->cv.wait(lk, [this]{return ready;});
相关文章:
- C++错误:"error: int aaa::bbb is protected within this context"
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 错误:类没有名为"ThisW"的成员;你是说"这个"吗?但是代码没有说"ThisW",它已经说了"This"
- C++ 中的构造函数、继承、堆栈、堆、this-pointer 和段错误
- C++ Boost - 序列化错误 - 将"const B"作为"this"参数
- 整体模板参数。错误:在常量表达式中使用'this'
- 如何解决"cout was not declared in this scope"错误?
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- 即使包含 <time.h 后仍"‘CLOCK_PER_SEC’ was not declared in this scope"错误>
- 使用 boost::序列化代码将 *this 传递给模板函数会产生错误
- С 错误"s1, s2 are used uninitialised in this function"
- 有没有办法绕过错误"因为'this'参数会丢弃限定符?
- 为什么直接传递"this"指针来存档是一个错误,而另一个相同类型的指针是可以的?
- "this" Lambda 捕获的是不正确的。海湾合作委员会编译器错误?
- C++ 中的"this declaration has no storage class or type specifier"错误
- Windows 10 环境中的"This version of XAudio2 is available only in Windows 8"错误
- C++错误:将"const umap_int {aka const std::unordered_map<int, int>}"作为"this"参数会丢弃限定符 [-permissive]
- 错误:将 x 作为 x 的参数传递'this'丢弃限定符
- 尝试使用指针时出现预期表达式"this"错误
- 修复 C++ 中的"this"错误