OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 4:11 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Solved: C++ Member Function to Function Pointer Argument
PostPosted: Fri May 05, 2017 12:05 pm 
Offline
Member
Member
User avatar

Joined: Fri Aug 07, 2015 6:13 am
Posts: 1134
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:
void (*function_pointer)();


Also I have a function that does all the "adding":
Code:
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:
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.

_________________
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader


Last edited by Octacone on Sat May 06, 2017 4:30 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: C++ Member Function to Function Pointer Argument
PostPosted: Fri May 05, 2017 2:45 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
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


Top
 Profile  
 
 Post subject: Re: C++ Member Function to Function Pointer Argument
PostPosted: Fri May 05, 2017 3:21 pm 
Offline
Member
Member
User avatar

Joined: Fri Aug 07, 2015 6:13 am
Posts: 1134
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


Top
 Profile  
 
 Post subject: Re: C++ Member Function to Function Pointer Argument
PostPosted: Fri May 05, 2017 3:32 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
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


Top
 Profile  
 
 Post subject: Re: C++ Member Function to Function Pointer Argument
PostPosted: Fri May 05, 2017 3:37 pm 
Offline
Member
Member
User avatar

Joined: Fri Aug 07, 2015 6:13 am
Posts: 1134
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:
#ifndef COMMANDS_H
#define COMMANDS_H
...includes...snip...

namespace Basic_OS
{
   class Commands_Class
   {
      public:
         void Help();
   };
}

#endif


Code:
#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:
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


Top
 Profile  
 
 Post subject: Re: C++ Member Function to Function Pointer Argument
PostPosted: Fri May 05, 2017 5:39 pm 
Offline
Member
Member
User avatar

Joined: Mon Mar 05, 2012 11:23 am
Posts: 616
Location: Germany
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:
class Commands {
public:
   static void Help() {
      printf("HELP\n");
   }
};


void callThatStaticMethod(void(*method)()) {
   method();
}

int main() {
   callThatStaticMethod(Commands::Help);
}

_________________
Ghost OS - GitHub


Top
 Profile  
 
 Post subject: Solved: C++ Member Function to Function Pointer Argument
PostPosted: Sat May 06, 2017 4:28 am 
Offline
Member
Member
User avatar

Joined: Fri Aug 07, 2015 6:13 am
Posts: 1134
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:
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


Top
 Profile  
 
 Post subject: Re: Solved: C++ Member Function to Function Pointer Argument
PostPosted: Sat May 06, 2017 7:11 am 
Offline
Member
Member
User avatar

Joined: Thu Jul 12, 2012 7:29 am
Posts: 723
Location: Tallinn, Estonia
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group