#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.呢?哪儿错了呢?请教各位大侠!

解决方案 »

  1.   

    cout<<不支持你的 class  Date 改成
    cout<<"My birthday is "<<me.GetBirthday().int GetYear()<<"-"<<me.GetBirthday().int GetMonth()<<"-"<<me.GetBirthday().int GetDay()<<endl;
      

  2.   

    多考了函数的返回值,更正一下cout<<"My birthday is "<<me.GetBirthday().GetYear()<<"-"<<me.GetBirthday().GetMonth()<<"-"<<me.GetBirthday().GetDay()<<endl;
      

  3.   

    Date类需要重载<<操作符,不然编译器不能理解
    cout<<"My birthday is "<<me.GetBirthday()<<endl;
    这一句在<<操作Date对象的时候应该怎么处理
      

  4.   

    <<运算符并不是所有的data类型都可用像你的例子中的birthday定义为Data类型变量,但Data类中并没有重载运算符
    所以处理方法一是 EnochShen(小疯子:真的好菜—知耻而后勇!)&pomelowu(羽战士)所说的
    使用GetDay(),GetYear()函数返回int类型值,这样int类型<<可以支持二就是自己重载<<运算符,使其可以直接支持Data类型变量的输出