Welcome to the next pikoTutorial !
The error we’re handling today is a C++ compilation error:
invalid use of non-static member functionWhat does it mean?
It often occurs when the compiler encounters a place in the code where it would normally expect a static member function, but the actual function is not static. Let’s say for example, that we have a function which takes a function pointer to some other function and calls it:
void RunFunction(void (*func)()) { func(); }If you create a SomeClass class with a non-static Run function and try to pass it to the RunFunction function using the range operator like below:
struct SomeClass { void Run() { std::cout << "Running..." << std::endl; } }; int main() { RunFunction(SomeClass::Run); }the compiler will throw an error:
error: invalid use of non-static member function ‘void SomeClass::Run()’How to fix it?
The easiest way is to just make Run function static, by adding static keyword to its definition:
static void Run()However, if you can’t make that function static, you would have to adjust the RunFunction function to accept some callable object instead of a raw function pointer:
void RunFunction(std::function<void()> func) { func(); }And bind SomeClass::Run function to a specific object instance using std::bind:
SomeClass instance; RunFunction(std::bind(&SomeClass::Run, instance));Or a simple lambda:
SomeClass instance; RunFunction([&instance]{ instance.Run(); });








