#include<iostream> using namespace std; class date{ public: friend void print(); date(){ day=1; month=2; year=3; } ~date(){ cout<<"bye"; } int setdate(int x,int y, int z){ day=x; month=y; year=z; } private: int day,month,year; }; void print(){ cout<<day<<month<<year<<endl; } int main(){ date a; print(); a.setdate(5,8,9); print(); }
解决方案
40
你好歹也传个对象给print当参数,不然怎么打印?
#include<iostream> using namespace std; class date{ public: friend void print(date &a); date(){ day=1; month=2; year=3; } ~date(){ cout<<"bye"; } void setdate(int x,int y, int z){ day=x; month=y; year=z; } private: int day,month,year; }; void print(date &a){ cout<<a.day<<a.month<<a.year<<endl; } int main(){ date a; print(a); a.setdate(5,8,9); print(a); }