I have written a simple class, myshape
, with a class method named display_area()
that prints area of a rectangle for N
number of times where N
will be provided by the user. I want this function to run in a thread independently. However while implementing threading I get error saying
error: invalid use of non-static member function
std::thread t1(s.display_area, 100);
I have seen the related discussion C++ std::thread and method class! where object instances have been created as a pointer unlike my case and could not able to resolve my problem. I am appending my code below for reference. Any help is appreciated.
#include <iostream>
#include <thread>
using namespace std;
class myshape{
protected:
double height;
double width;
public:
myshape(double h, double w) {height = h; width = w;}
void display_area(int num_loop) {
for (int i = 0; i < num_loop; i++) {
cout << "Area: " << height*width << endl;
}
}
};
int main(int argc, char** argv)
{
myshape s(5, 2);
s.print_descpirtion();
std::thread t1(s.display_area, 100);
t1.join();
}