#include<iostream.h>
#include<string.h>
class Date
{ public:
Date(int y=2000, int m=1,int d=1)
{ cout<<"Date constructor\n"; year=y; month=m; day=d; }
Date(Date &d)
{ cout<<"Date copy constructor\n"; year = d.year; month = d.month; day = d.day; }
~Date() { cout<<"Date destructor\n"; }
int GetYear(){ return year; }
int GetMonth(){ return month; }
int GetDay(){ return day; }
private:
int year; int month; int day;
};class Person
{ public:
Person(char *np, Date b);
~Person() { cout<<"Person destructor\n"; }
char * GetName() { return name; }
Date GetBirthday() { return birthday; }
private:
char name[20];
Date birthday;
};
Person::Person (char *np,Date b):birthday(b)
{ cout<<"Person constructor\n";
strcpy(name,np); }
void main()
{ Date nationalday(1980,10,1), birthday( nationalday );
char name[20]="Li qiang";
Person me( name, birthday );
cout<<"My name is "<<me.GetName()<<endl;
cout<<"My birthday is "<<me.GetBirthday()<<endl;
}为什么会显示:text8.cpp(36) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Date' (or there is no acceptable conversion)
Error executing cl.exe.呢?哪儿错了呢?请教各位大侠!
#include<string.h>
class Date
{ public:
Date(int y=2000, int m=1,int d=1)
{ cout<<"Date constructor\n"; year=y; month=m; day=d; }
Date(Date &d)
{ cout<<"Date copy constructor\n"; year = d.year; month = d.month; day = d.day; }
~Date() { cout<<"Date destructor\n"; }
int GetYear(){ return year; }
int GetMonth(){ return month; }
int GetDay(){ return day; }
private:
int year; int month; int day;
};class Person
{ public:
Person(char *np, Date b);
~Person() { cout<<"Person destructor\n"; }
char * GetName() { return name; }
Date GetBirthday() { return birthday; }
private:
char name[20];
Date birthday;
};
Person::Person (char *np,Date b):birthday(b)
{ cout<<"Person constructor\n";
strcpy(name,np); }
void main()
{ Date nationalday(1980,10,1), birthday( nationalday );
char name[20]="Li qiang";
Person me( name, birthday );
cout<<"My name is "<<me.GetName()<<endl;
cout<<"My birthday is "<<me.GetBirthday()<<endl;
}为什么会显示:text8.cpp(36) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Date' (or there is no acceptable conversion)
Error executing cl.exe.呢?哪儿错了呢?请教各位大侠!
cout<<"My birthday is "<<me.GetBirthday().int GetYear()<<"-"<<me.GetBirthday().int GetMonth()<<"-"<<me.GetBirthday().int GetDay()<<endl;
cout<<"My birthday is "<<me.GetBirthday()<<endl;
这一句在<<操作Date对象的时候应该怎么处理
所以处理方法一是 EnochShen(小疯子:真的好菜—知耻而后勇!)&pomelowu(羽战士)所说的
使用GetDay(),GetYear()函数返回int类型值,这样int类型<<可以支持二就是自己重载<<运算符,使其可以直接支持Data类型变量的输出