类访问友元函数的定义?请指出错误的地方并修正

来源:百度知道 编辑:UC知道 时间:2024/06/17 11:52:40
定义文件a.h如下:
#ifndef _A_H
#define _A_H
#include <iostream>
using namespace std;
class B;
class A
{
public:
A(int in):_next(0)
{
n=in;
}
void display()
{
cout<<n<<endl;
}
friend int Add(A *ptr);
friend void Display();
private:
int n;
A* _next;
};
#endif
定义b.h如下:
#ifndef _B_H
#define _B_H
#include <iostream>
using namespace std;
class A;
class B
{
public:
B():_head(0),_end(0),_ptr(0)
{}
int Add(A *ptr)
{
if(0==_head)
{
_head=_end=ptr;
}
else
{
_end->_next=ptr;
}
return 1;
}
void Display()
{
if(0==_head)
cout<<"empty"<<endl;
else
{
A *ptr=_head;
while(ptr)

需要这样修改(错误地方用注释说明了):
别外逻辑错误也给你改过来了。

定义文件a.h如下:
*************************************************
#ifndef _A_H
#define _A_H

#include <iostream>
using namespace std;

//需要B的实现,加上include
#include "B.h"

class A
{
public:
A(int in):_next(0)
{
n=in;
}

void display()
{
cout<<n<<endl;
}

//这里是B的成员函数,需要加上类域B::
friend int B::Add(A *ptr);
friend void B::Display();
private:
int n;
A* _next;
};
#endif
***********************************************

定义b.h如下:
*************************************************
#ifndef _B_H
#define _B_H
//这里只声明类A,在CPP中再include
class A;
class B
{
public:
B():_head(0),_end(0),_ptr(0)
{}
int Add(A *ptr);

void Display();

private: <