试图使用已删除的函数
Thread (Attempt to use a deleted function
我正在跟随一个关于线程的在线教程,并得到了错误消息"语义问题:试图使用已删除的函数"。知道怎么了吗?
#include <iostream>
#include <thread>
#include <string>
using namespace std;
class Fctor {
public:
void operator() (string & msg) {
cout << "t1 says: " << msg << endl;
msg = "msg updated";
}
};
int main(int argc, const char * argv[]) {
string s = "testing string " ;
thread t1( (Fctor()), s);
t1.join();
return 0;
}
好吧,代码与VS2015, MS-Compiler一起工作,对代码进行以下更改:
void operator() (string & msg) {
cout << "t1 says: " << msg << endl;
msg = "msg updated";
}
void operator() (std::string& msg) {
std::cout << "t1 says: " << msg.c_str() << std::endl;
msg = "msg updated";
}
和
string s = "testing string " ;
thread t1( (Fctor()), s);
std::string s = "testing string ";
Fctor f;
std::thread t1(f, s);
我更改的两个主要内容是msg.c_str(),因为流不接受string,而是接受const char*。其次,我将RValue因子()转换为LValue因子f并将f作为参数,线程显然不接受RValue。
相关文章:
- 为什么我的双向链表删除函数会删除多个节点?
- unique_ptr实现接口时对已删除函数的引用
- 可视代码 删除函数括号内的空格
- 从尝试引用已删除函数的矢量 C++ 中删除对象
- 试图引用已删除函数数组的相等运算符
- 双链表堆栈删除函数不起作用
- 是否需要删除函数中未使用的新结构?
- C++引用已删除函数错误
- 删除函数环境中C++输入 R 对象
- 删除函数中的对象C++
- 在 C++ 中使用删除函数的不同方式
- C++错误:C2280 - 引用已删除函数的编译器错误?
- C++,删除函数/迭代进程中定义的动态数组
- 删除函数 c++ 的读取访问冲突异常
- 如何对此自定义C 列表类实现删除函数
- 双向链表问题(特别是复制构造函数和删除函数)
- 引用 std::atomic <bool>的已删除函数错误
- 在删除函数自由度的指针后,为什么我会得到核心转储
- C++删除函数
- 返回 C++11 中已删除函数的类型