class Producer
{
public:
Producer(){
}
void Shout(){
for(int i=0;i<10;i++){
printf("I am a producer!!\n");
}
}
};
void ThreadTest()
{
void (Producer::* ptfptr) () = &Producer::Shout;
Producer prod;
(prod.*ptfptr) ();
Thread *pt = new Thread("producer");
pt->Fork((prod.*ptfptr)(),0);
}
I am trying to create a producer thread in nachos and and for that i am creating a class Producer
(necessary for my assignment). I have a non-static member function Shout()
in the class declaration, and I used the above code to create and use the function pointer to the Shout()
method. The compiler gives me "invalid use of non-static member function". Where is the mistake here?
You didn't say which line the compiler is complaining about, but I'm going to guess that it's this one:
void (Producer::* ptfptr) () = &Producer::Shout;
Here you are creating a function pointer using the address of a non-static function. Non-static functions need an object to operate on, and you don't have an object yet.
Have a look at this question and its top answer for a good example of how to do what you're looking for.