Solved: C++ Member Function to Function Pointer Argument

Programming, for all ages and all languages.
Post Reply
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Solved: C++ Member Function to Function Pointer Argument

Post 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:

Code: Select all

void (*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.
Last edited by Octacone on Sat May 06, 2017 4:30 am, edited 1 time in total.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: C++ Member Function to Function Pointer Argument

Post 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.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: C++ Member Function to Function Pointer Argument

Post 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.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: C++ Member Function to Function Pointer Argument

Post 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.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: C++ Member Function to Function Pointer Argument

Post 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();
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: C++ Member Function to Function Pointer Argument

Post 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);
}
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Solved: C++ Member Function to Function Pointer Argument

Post 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!!! =D>
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. :oops:
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Solved: C++ Member Function to Function Pointer Argument

Post 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.
Learn to read.
Post Reply