我正在关注有关线程的在线教程,并收到错误消息“语义问题:尝试使用已删除的函数”。知道有什么问题吗?
#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(),因为流不使用字符串,而是const char *。
其次,我将RValue Fctor()转换为LValue Fctor f并赋予f作为参数,该线程显然不采用RValues。
关于c++ - 线程(尝试使用已删除的功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32171954/