【发布时间】:2018-07-12 05:20:21
【问题描述】:
我正在尝试执行此代码。我认为这很简单,但我遇到了这个错误,我无法弄清楚:
// THIS IS THE MAIN FILE ////////
/////////////////////////////////
#include <iostream>
#include "sdf_func.hpp"
#include "single_task_func.hpp"
using namespace std;
int main() {
sdf obj1;
s_task cgh;
cgh.single_task([=] {
for (int i=0; i<30; i++) {
obj1.sdf_write(10);
};
});
cgh.single_task([=] {
for (int i=0; i<30; i++) {
obj1.sdf_write(10);
};
});
return 0;
};
// THIS IS SDF_FUNC.HPP ////////////////////
////////////////////////////////////////////
#include <iostream>
using namespace std;
class sdf {
int done;
public:
sdf() : done(0) {};
void sdf_write (int size) {
static int wr_count = 0;
if (wr_count == size) {
done++;
}
wr_count++;
cout << wr_count;
};
};
// THIS IS SINGLE_TASK_FUNC.HPP///////////////////
//////////////////////////////////////////////////
#include <iostream>
#include <thread>
using namespace std;
class s_task {
struct task {
void schedule (std::function<void(void)> f) {
auto execution = [=] { f(); };
std::thread thread(execution);
thread.detach();
};
};
task *task1;
public:
void single_task(std::function<void(void)> F) {
task1->schedule(F);
}
};
我正在尝试运行 2 线程。但是由于某种原因,当我尝试从 main 调用 lambda 函数“single_task”时。它给了我这个错误:
错误:将“const sdf”作为“this”参数传递会丢弃限定符 [-fpermissive] obj1.sdf_write(10);
【问题讨论】:
-
您不能在 const 对象上调用
non-const成员函数。
标签: c++