Page 1 of 1
Solved: C++ Member Function to Function Pointer Argument
Posted: Fri May 05, 2017 12:05 pm
by Octacone
Right now I am working on my shell implementation, and I developed an easy system for adding new commands.
Basically I have a command structure that contains some information about that specific command, including a function pointer:
Also I have a function that does all the "adding":
Code: Select all
void Command_List_Add_Command(char* name, char* description, void (*function_pointer)());
//Assignment
command_list[command_count].function_pointer = function_pointer;
There is a function that contains all the add calls. //This is the problem
Code: Select all
Command_List_Add_Command("help", "Displays all available commands.", Commands.Help);
So, I keep getting an error saying: "error: invalid use of non-static member function", even if I make it static.
Side note: all the commands are located in a separate C++ file. "
Commands" is an instance/object of "Commands_Class" class.
I tried everything I remembered of, nothing seemed to be okay. I even trying declaring an external "C" virtual function, that did compile but I was getting opcode exceptions, general protection fault exceptions, etc...
I would appreciate any help.
Re: C++ Member Function to Function Pointer Argument
Posted: Fri May 05, 2017 2:45 pm
by eryjus
There's a lot of details missing here, but I'm going to bet you are either using the class name to qualify the method call rather than the instance name or calling the function from within a static method. In short, the error is telling you that there is no instance to operate on.
Re: C++ Member Function to Function Pointer Argument
Posted: Fri May 05, 2017 3:21 pm
by Octacone
eryjus wrote:There's a lot of details missing here, but I'm going to bet you are either using the class name to qualify the method call rather than the instance name or calling the function from within a static method. In short, the error is telling you that there is no instance to operate on.
It is neither of those. I tried almost everything. Static and non-static, with the instance and without. I actually don't even know if this is possible. What details am I missing? Only thing I didn't include was the commands file.
Re: C++ Member Function to Function Pointer Argument
Posted: Fri May 05, 2017 3:32 pm
by eryjus
eryjus wrote:There's a lot of details missing here
Octacone wrote:What details am I missing?
How about the Commands_Class class definition and the function responsible for calling the member in question? And anything else that will help us. We do not have the benefit of seeing your code in context and what you have posted is not enough to determine the problem.
Re: C++ Member Function to Function Pointer Argument
Posted: Fri May 05, 2017 3:37 pm
by Octacone
eryjus wrote:eryjus wrote:There's a lot of details missing here
Octacone wrote:What details am I missing?
How about the Commands_Class class definition and the function responsible for calling the member in question? And anything else that will help us. We do not have the benefit of seeing your code in context and what you have posted is not enough to determine the problem.
Code: Select all
#ifndef COMMANDS_H
#define COMMANDS_H
...includes...snip...
namespace Basic_OS
{
class Commands_Class
{
public:
void Help();
};
}
#endif
Code: Select all
#include "Headers/Kernel/Commands.h"
using namespace Basic_OS;
extern Text_User_Interface TUI;
extern Shell_Class Shell;
void Commands_Class::Help() //tried defining it as static but didn't help :|
{
//Irrelevant code
}
eryjus wrote:the function responsible for calling the member in question?
I guess you wanted this part?
Code: Select all
void (*function)();
function = command_list[i].function_pointer;
function();
Re: C++ Member Function to Function Pointer Argument
Posted: Fri May 05, 2017 5:39 pm
by max
Your "Help" method is not static and you must use the :: operator for static methods of a class. You can not pass a pointer to the method of a class instance. That would be a lambda.
Code: Select all
class Commands {
public:
static void Help() {
printf("HELP\n");
}
};
void callThatStaticMethod(void(*method)()) {
method();
}
int main() {
callThatStaticMethod(Commands::Help);
}
Solved: C++ Member Function to Function Pointer Argument
Posted: Sat May 06, 2017 4:28 am
by Octacone
max wrote:Your "Help" method is not static and you must use the :: operator for static methods of a class. You can not pass a pointer to the method of a class instance. That would be a lambda.
Code: Select all
class Commands {
public:
static void Help() {
printf("HELP\n");
}
};
void callThatStaticMethod(void(*method)()) {
method();
}
int main() {
callThatStaticMethod(Commands::Help);
}
Thank you so so much for helping me!!!
I figured it out, it was such a shameful mistake. Instead of declaring functions as static inside the header I was doing it inside the source file. I thought it was the other way around this entire time. I don't use static functions quite often.
Re: Solved: C++ Member Function to Function Pointer Argument
Posted: Sat May 06, 2017 7:11 am
by dozniak
If the functions inside Commands class are all static there's no point in keeping it a class, just make it a namespace.
There are things like mem_fn if you want to address non-static members of a class.
cppreference wrote:
Function template std::mem_fn generates wrapper objects for pointers to members, which can store, copy, and invoke a pointer to member. Both references and pointers (including smart pointers) to an object can be used when invoking a std::mem_fn.