"Member function must be called or its address taken in function" C++ error

440 views Asked by At

I am writing a program in C++ which works with threads. When I try to create a process I get the following error: Member function must be called or its address taken in function. Here is my code:

void PCB::createProcess(){
    asm cli
    this->stack = new unsigned[stackSize];

    stack[stackSize-1] = 0x200;             
    stack[stackSize-2] = FP_SEG(wrapper);
    stack[stackSize-3] = FP_OFF(wrapper);
    this->ss = FP_SEG(stack+stackSize-12);
    this->sp = FP_OFF(stack+stackSize-12);

    asm sti
}


void PCB::wrapper(){

    myThread->run();

    myThread->state = TERMINATED;
}

I get the error in these two lines:

stack[stackSize-2] = FP_SEG(wrapper);
stack[stackSize-3] = FP_OFF(wrapper);

I have tried everything. I have tried (&(wrapper)), PCB::wrapper, &PCB::wrapper and nothing helps, it just gives me more errors. PCB is the name of the class.

If anyone has an idea, please help.

1

There are 1 answers

2
Pete Becker On

FP_SEG and FP_OFF are macros that extract the segment selector and the offset, respectively, from a pointer. That's how we manipulated pointers under DOS and early versions of Windows, twenty-plus years ago. Unless you're targeting some funky system that you haven't mention, they are totally inappropriate.

Since PCB::wrapper is a member function, a pointer to it is a pointer-to-member-function. It is not an ordinary function, so the compiler is right to complain that it can't make sense out of what the code is trying to do.

These days, multithreading is done with std::thread. You don't have to do that hacky stack manipulation; just pass the pointer-to-member-function and the this pointer to the constructor for std::thread.

std::thread thr(&PCB::wrapper, this);