C++ “朋友法”;未在此范围内声明”;在C++;
首先提供一些上下文,这是针对涉及信号量的赋值。我们要找到哲学家进餐问题的代码,让它工作,然后执行一些分析和操作。然而,我遇到了一个错误 原始代码取自 使用C++解决方案。 我在Code::Blocks中收到的错误是C++ “朋友法”;未在此范围内声明”;在C++;,c++,class,scope,friend,C++,Class,Scope,Friend,首先提供一些上下文,这是针对涉及信号量的赋值。我们要找到哲学家进餐问题的代码,让它工作,然后执行一些分析和操作。然而,我遇到了一个错误 原始代码取自 使用C++解决方案。 我在Code::Blocks中收到的错误是 philosopher.cpp|206|error: 'Philosopher_run' was not declared in this scope| 该错误发生在以下行中: if ( pthread_create( &_id, NULL, (void *(*)(void
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 )
类哲学家
{
私人:
pthread_t_id;
整数;
国际时间生活;
公众:
哲学家(空){u数=-1;{u时间生活=0;};
哲学家(intn,intt){{u number=n;{u timeToLive=t;};
~哲学家(虚空){};
作废筷子(作废);
无效释放筷子(无效);
作废开始(作废);
无效等待(void);
朋友虚空哲学家_run(哲学家*p);
};
void:开始(void)
//启动代表哲学家的线程
{
如果(_数<0)
{
cerr如果没有依赖于参数的查找,朋友声明不会使朋友的名称可见
§7.3.1.2[namespace.memdef]p3
[…]如果非局部类中的friend
声明首先声明了一个类或函数,则friend类或函数是最内层封闭命名空间的成员。在该命名空间范围中提供匹配声明之前,通过非限定查找或限定查找不会找到friend的名称。(在给予友谊的阶级定义之前或之后)。[…]
这意味着您应该将void哲学家\u run(哲学家*p);
放在类之前(连同哲学家的前向声明一起),或者放在类之后(同时将友元声明保留在类中).如果没有依赖于参数的查找,朋友声明不会使朋友的名称可见
§7.3.1.2[namespace.memdef]p3
[…]如果非局部类中的friend
声明首先声明了一个类或函数,则friend类或函数是最内层封闭命名空间的成员。在该命名空间范围中提供匹配声明之前,通过非限定查找或限定查找不会找到friend的名称。(在给予友谊的阶级定义之前或之后)。[…]
这意味着您应该将void phisor_run(phisor*p);
放在类之前(连同phisor
的前向声明),或者放在类之后(同时将友元声明保留在类中)