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?