0

Question

The question as follows, and I tried many methods, but I don not even know how to fix it until now.

fizz-buzz-multithreaded.cc: In function ‘int main(int, const char**)’:
fizz-buzz-multithreaded.cc:95:43: error: invalid use of non-static member function ‘void FizzBuzz::fizz(std::function<void()>)’
     thread tfizz(fizzbuzz->fizz, printFizz);
                                           ^
fizz-buzz-multithreaded.cc:21:10: note: declared here
     void fizz(function<void()> printFizz) {
          ^~~~
fizz-buzz-multithreaded.cc:96:43: error: invalid use of non-static member function ‘void FizzBuzz::buzz(std::function<void()>)’
     thread tbuzz(fizzbuzz->buzz, printBuzz);
                                           ^
fizz-buzz-multithreaded.cc:36:10: note: declared here
     void buzz(function<void()> printBuzz) {
          ^~~~
fizz-buzz-multithreaded.cc:97:55: error: invalid use of non-static member function ‘void FizzBuzz::fizzbuzz(std::function<void()>)’
     thread tfizzbuzz(fizzbuzz->fizzbuzz, printFizzBuzz);
                                                       ^
fizz-buzz-multithreaded.cc:51:10: note: declared here
     void fizzbuzz(function<void()> printFizzBuzz) {
          ^~~~~~~~
fizz-buzz-multithreaded.cc:98:49: error: invalid use of non-static member function ‘void FizzBuzz::number(std::function<void(int)>)’
     thread tnumber(fizzbuzz->number, printNumber);
                                                 ^
fizz-buzz-multithreaded.cc:66:10: note: declared here
     void number(function<void(int)> printNumber) {
          ^~~~~~

Source Code

This is a LeetCode programming question, and the error occurred in main function in my source code.

main function as follows:

int main(int argc, char const *argv[]) {
    auto fizzbuzz = new FizzBuzz(15);

    function<void()> printFizz = [] { cout << "fizz" << endl; };
    function<void()> printBuzz = [] { cout << "buzz" << endl; };
    function<void()> printFizzBuzz = [] { cout << "fizzbuzz" << endl; };
    function<void(int)> printNumber = [](int count) { cout << count << endl; };

    thread tfizz(fizzbuzz->fizz, printFizz);
    thread tbuzz(fizzbuzz->buzz, printBuzz);
    thread tfizzbuzz(fizzbuzz->fizzbuzz, printFizzBuzz);
    thread tnumber(fizzbuzz->number, printNumber);

    tfizz.join();
    tbuzz.join();
    tfizzbuzz.join();
    tnumber.join();
    delete fizzbuzz;
    return 0;
}

The class template as follows:

class FizzBuzz {
private:
    int n;
public:
    FizzBuzz(int n) { this->n = n; }
    // printFizz() outputs "fizz".
    void fizz(function<void()> printFizz) { ... }
    // printBuzz() outputs "buzz".
    void buzz(function<void()> printBuzz) { ... }
    // printFizzBuzz() outputs "fizzbuzz".
    void fizzbuzz(function<void()> printFizzBuzz) { ... }
    // printNumber(x) outputs "x", where x is an integer.
    void number(function<void(int)> printNumber) { ... }
};
  • 1
    The problem is not related to the use of `std::function`. The problem are the first arguments in the initializers, e.g `fizzbuzz->fizz` in `thread tfizz(fizzbuzz->fizz, printFizz);`. You cannot pass non-static member functions like this. See duplicate for resolution. – walnut Apr 03 '20 at 13:21
  • TL;DR: the syntax you are looking for is `thread tfizz(&FizzBuzz::fizz, fizzbuzz, printFizz);` – Yksisarvinen Apr 03 '20 at 13:23
  • ```thread tfizz(&FizzBuzz::fizz, fizzbuzz, printFizz);``` is worked. Thanks for [Yksisarvinen](https://stackoverflow.com/users/7976805/yksisarvinen) – FisherCloud Apr 06 '20 at 10:29

0 Answers0