0

I have the following code:

#include <iostream>
#include <vector>
#include <string>
#include <functional>
using namespace std;

struct AStruct {
    string astr;
};

class OtherClass1 {
    public:
        OtherClass1(string s) : str(s) {};
        string str; 
};

class OtherClass2 {
    public:
        OtherClass2(string s) : str(s) {};
        string str; 
};

struct BStruct {
    vector<OtherClass1> vOC1;
    vector<OtherClass2> vOC2;
};


class MainClass {
    public:
        MainClass() {};
        void Update(const BStruct& BS) {
            AStruct v1 = Func(Func1, BS.vOC1);
            AStruct v2 = Func(Func2, BS.vOC2);
            cout << "v1 = " << v1.astr << endl;
            cout << "v2 = " << v2.astr << endl;
        }

    private:
        AStruct Func1(const OtherClass1& oc1) { 
            AStruct AS;
            AS.astr = oc1.str + " oc1 ";
            return AS;
        }
        AStruct Func2(const OtherClass2& oc2) { 
            AStruct AS;
            AS.astr = oc2.str + " oc2 ";
            return AS;
        }
        // AStruct Func3(const OtherClass3& oc3); ...

        template <typename T>
        AStruct Func(function<AStruct(const T&)> Funky, const vector<T>& Foos) {
            AStruct ast;
            for (size_t i = 0; i < Foos.size(); ++i) 
                ast = Funky(Foos[i]);
            return ast;
        }
};

Please ignore the silliness of the functions, it was the simplest I could come up with.. The idea is that Func performs some sequence of logic on objects of class OtherClass1 and OtherClass2 where the objects of those classes are handled by a different function and usually come in vector form. I get an error compiling:

error: invalid use of non-static member function ‘AStruct MainClass::Func1(const OtherClass1&)’
   43 |             AStruct v1 = Func(Func1, BS.vOC1);

While this is probably a terrible solution (a consequence of poor design), why do I get the above error?

algae
  • 407
  • 4
  • 15
  • 1
    Does it have to be non-static? The Main Class doesn't have any variable members from what I can see. You can always make it a ```static AStruct Func1(...``` and call it a day. – Roy2511 Oct 21 '19 at 05:24
  • Maybe it does need to be non-static as `Func1/2` use member variables from `MainClass` (I didn't include them in this example, sorry). But what is this error about anyway? What's special about passing a function as a parameter. – algae Oct 21 '19 at 06:25

1 Answers1

1

Type of some functions:

-&MainClass::Func1 : std::function<Astruct(const OtherClass1&)>*

-&MainClass::Func2 : std::function<Astruct(const OtherClass2&)>*

Two ways to resolve this.

  1. Change the parameter types

  2. Use lambda functions

Related post: std::thread calling method of class

khanh
  • 600
  • 5
  • 20
  • This doesn't seem to work out of the box. Did you change something in the definition of `Func`? It gives an argument mismatch error (`no instance of function template MainClass::Func...`). – algae Oct 21 '19 at 08:23
  • I think you can take a look at https://stackoverflow.com/questions/10998780/stdthread-calling-method-of-class , Method in C++ is a bit different from pure function – khanh Oct 21 '19 at 13:46