[英]Creating a thread in NachOS using a C++ class
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). 我正在尝试在玉米片中创建一个生产者线程,为此,我正在创建一个类
Producer
(我的作业必需)。 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. 我在类声明中有一个非静态成员函数
Shout()
,并且我使用了上面的代码来创建和使用指向Shout()
方法的函数指针。 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. 看看这个问题及其最重要的答案,这是一个很好的例子,说明如何做您想要的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.