【发布时间】:2017-05-01 06:09:29
【问题描述】:
在将此标记为重复之前,我已经看到了其他答案,但它们并没有解决我的问题。
我有两个类如下:
A.cpp:
class A
{
public:
A();
int getValue()//just an example of a get method
{
return value;
}
private:
int value;
// a lot of variables
}
B.cpp:
class B
{
public:
B();
void addData(string fileName)
{
A* a = new A();
//reads the file with the fileName and does alot of stuff
//after calculation is over it adds the object to the vector
list.push_back(a);
}
void run()
{
thread t1(&B::simulate, list[0]);
thread t2(&B::simulate, list[1]);
t1.join();
t2.join();
}
private:
vector<A*> list;
void simulate(A* ptr)
{
int value = 0;
cout << "At first value is " << value << endl;
weight = ptr->getValue();
cout << "Then it becomes " << value << endl;
}
}
然后我有一个简单的 main.cpp:
int main()
{
B* b = new B();
b->addData("File1.txt");
b->addData("File2.txt");
b->run();
return 0;
}
我正在尝试通过调用 run() 方法来创建两个线程。但是,当我尝试编译时,出现以下错误:
error C2672: 'std::invoke': no matching overloaded function found
我检查了其他帖子,但似乎没有什么对我有用。任何帮助将不胜感激。
P.S:我正在使用以下包括:
#include <thread>
#include <iostream>
还有:
using namespace std;
我正在使用其他包含,但它们无关紧要
【问题讨论】:
-
那是很多空白......
标签: c++ multithreading visual-studio compiler-errors