注册 登录
编程论坛 C++教室

写了个关于友元成员函数的 运行的时候出错了.各位参谋下.

liuweizwh 发布于 2013-05-05 13:31, 441 次点击
#include<iostream>
#include<string>
using namespace std;
class Student;
class Date
{
private:
    int year,month,day;
public:
    Date(int y,int m,int d);
    void display(const Student &stu);
};
class Student
{
private:
    char *specialty;
public:
    Student (char *pspec);
    ~Student();
    friend void Date::display(const Student &);
};
Date::Date(int y,int m,int d)
{
    cin>>y>>" ">>m>>" ">>d;
    year=y;
    month=m;
    day=d;
}
void Date::display(const Student &stu)
{
    cout<<stu.specialty<<endl;
    cout<<year<<" "<<month<<" "<<day<<endl;
}
Student::Student(char *pspec)
{
    if(pspec)
    {
        specialty=new char[strlen(pspec)+1];
        strcpy(specialty,pspec);
    }
    else specialty=0;
}
Student::~Student()
{
    if(specialty)
        delete []specialty;
    else specialty=0;
}

int main()
{
    int y,m,d;
    cin>>y>>" ">>m>>" ">>d;
    Student zhang ("computer");
    Date birthday (y,m,d);
    birthday. display (zhang);
    return 0;
}
 运行的时候我输入日期 按回车 然后就突然中止了



















1 回复
#2
liuweizwh2013-05-05 13:48
自己解决了
1