友元问题 在执行的时候出现错误(DC++和VC++6.0)
#include<iostream>#include<string.h>
using namespace std;
class person;
class spouse
{
private:
person *phusband;
person *pwife;
public:
spouse(const person &hus,const person &wf);
void Show()const;
~spouse(){
delete phusband;
delete pwife;
}
};
class person
{
private:
char *name;
int age;
char sex[6];
public:
person(char *n,int a,char *s):age(a)
{
strcpy(name,n);strcpy(sex,s);
}
void Show()const{
cout<<name<<""<<age<<sex<<endl;
}
friend void spouse::Show() const;
};
spouse::spouse(const person &hus,const person &wf)
{
phusband=new person(hus);
pwife=new person(wf);
}
void spouse::Show()const
{
cout<<"丈夫:"<<phusband->name<<phusband->age<<phusband->sex<<endl;
cout<<"妻子:"<<pwife->name<<pwife->age<<pwife->sex<<endl;
}
int main()
{
person husband("张强",32,"男");
person wife("吴珊",28,"女");
spouse sp(husband,wife);
wife.Show();
husband.Show();
sp.Show ();
system("pause");
return 0;
}