首先提供一些上下文,用于涉及信号量的分配。我们要找到哲学家哲学问题的代码,让它工作,然后进行一些分析和操作。但是,我陷入了一个错误。C++中的“未在此范围内声明”的朋友方法
原始代码取自http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/ 使用C++解决方案。
我在代码:: Blocks的接收错误是
philosopher.cpp|206|error: 'Philosopher_run' was not declared in this scope|
,并在该行出现此错误:
if (pthread_create(&_id, NULL, (void *(*)(void *)) &Philosopher_run,
this) != 0)
我已经看过了在pthread_create方法,但一直未能解决这个错误。如果任何人都可以解释如何纠正这个错误给我,也为什么会发生这个错误,我将不胜感激。我试图只提供相关的代码。
class Philosopher
{
private:
pthread_t _id;
int _number;
int _timeToLive;
public:
Philosopher(void) { _number = -1; _timeToLive = 0; };
Philosopher(int n, int t) { _number = n; _timeToLive = t; };
~Philosopher(void) {};
void getChopsticks(void);
void releaseChopsticks(void);
void start(void);
void wait(void);
friend void Philosopher_run(Philosopher* p);
};
void Philosopher::start(void)
// Start the thread representing the philosopher
{
if (_number < 0)
{
cerr << "Philosopher::start(): Philosopher not initialized\n";
exit(1);
}
if (pthread_create(&_id, NULL, (void *(*)(void *)) &Philosopher_run,
this) != 0)
{
cerr << "could not create thread for philosopher\n";
exit(1);
}
};
void Philosopher_run(Philosopher* philosopher)