vendredi 3 avril 2020

c++ compile error: invalid use of non-static member function(std::function as parameter in class member function) [duplicate]

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) { ... }
};

Aucun commentaire:

Enregistrer un commentaire