gpt4 book ai didi

C++:创建线程的问题; error C2672: 'std::invoke': 找不到匹配的重载函数

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:22 29 4
gpt4 key购买 nike

在将此标记为重复之前,我已经看过其他答案,但它们没有解决我的问题。

我有两个类如下:

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;

我正在使用其他包含,但它们无关紧要

最佳答案

B::simulate 是一个非静态成员函数,因此它需要 2 个参数 - thisptr,而您只提供一个.您应该将其重新声明为静态的,因为它无论如何都不会访问 this 类成员。

关于C++:创建线程的问题; error C2672: 'std::invoke': 找不到匹配的重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com