1

I have problem using Borland C++ compiler in Eclipse, I want to pass address of one member function to constructor of another class which is friend of first class

PCB::PCB(Thread *t, long stack, int time, void (*method)())

I'm using method to find FP_SEG and FP_OFF, but it's giving me errors when I write

Thread::Thread (StackSize stackSize, Time timeSlice){
    myPCB = new PCB(this, stackSize, timeSlice, run);
}

This is error I'm getting all the time:

Could not find a match for 'PCB::PCB(Thread * const,unsigned long,unsigned int,void)' in function Thread::Thread(unsigned long,unsigned int)

skypjack
  • 49,335
  • 19
  • 95
  • 187
Brana
  • 33
  • 4

1 Answers1

4

Although member function invocation looks like a regular function invocation, the two are not the same, because there is compiler trickery involved in passing the this pointer to the function being called. When you call a member-function from another member function, C++ fills in the this pointer for you. That is why member functions are incompatible with regular functions, only static functions are.

If you need to pass a function to an API that you did not write, you need to pass a regular function. Otherwise, consider changing the code to take std::function instead, because it is compatible with member functions.

Sometimes third-party C-style APIs support passing an extra parameter for the "context" of the call. If your API supports a void* as well, you could write a regular function that casts a void pointer coming back to an instance of your class, and call a member function on that instance.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523