朋友方法“未在此范围内声明”在C ++中 [英] Friend method "not declared in this scope" in C++

查看:240
本文介绍了朋友方法“未在此范围内声明”在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先提供一些背景信息,这是针对涉及信号量的作业。我们将找到餐饮哲学家问题的代码,使其正常运行,然后执行一些分析和处理。但是,我陷入了一个错误。

First to provide some context, this is for an assignment involving semaphores. We are to find code for the dining philosophers problem, get it working, and then perform some analysis and manipulation. However, I am stuck with an error.

原始代码来自 http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/
使用C ++解决方案。

The original code is taken from http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/ using the C++ solution.

我在Code :: Blocks中收到的错误是

The error I am receiving in Code::Blocks is

philosopher.cpp|206|error: 'Philosopher_run' was not declared in this scope|

,并且此错误发生在行中:

and this error occurs in the line:

if ( pthread_create( &_id, NULL, (void *(*)(void *)) &Philosopher_run,
         this ) != 0 )

我查找了pthread_create方法,但无法解决此错误。如果有人可以向我解释如何纠正此错误,以及为什么发生此错误,我将不胜感激。我试图只提供相关代码。

I have looked up the pthread_create method but have been unable to fix this error. If anyone could explain how to correct this error to me, and also why this error is occurring, I would greatly appreciate it. I have tried to provide only the relevant code.

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 )


推荐答案

在没有参数依赖查找的情况下,朋友声明不会使朋友的名字可见。

A friend declaration does not make the name of the friend visible without argument-dependant lookup.

§7.3.1.2[命名空间。 memdef] p3


[...]如果朋友声明首先声明一个类或函数,而该朋友类或函数是最内层封闭的命名空间的成员。 直到在该名称空间范围内提供匹配的声明(在授予友谊的类定义之前或之后),才能通过非限定查找或限定查找来找到朋友的名字。 [...]

[...] If a friend declaration in a nonlocal class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup or by qualified lookup until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship). [...]

意思是应该放置 void Philosopher_run(Philosopher * p); ,或者在类之前(连同 Philosopher 的前向声明)一起,或者在类之后(同时将朋友声明保留在类中)。

Meaning that you should put void Philosopher_run( Philosopher* p ); either before the class (together with a forward declaration of Philosopher), or after the class (while keeping the friend declaration inside the class).

这篇关于朋友方法“未在此范围内声明”在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆