1.cannot convert ‘this’ pointer from ‘const class Date’ to ‘class Date &’
原因:我开始以为是因为参数的const类型问题,就像char * 不能转成const char*。但是在实际代码调试中,发现问题出现在函数后的const上,const放在函数后面代表此函数不会改变成员变量。同时也是如果你函数体内部改变了变量值,则此const不能添加,例如:
bool Teacher::Iscontiwork()const{
int y=workdata.getyear();
if(Isretire() && 2016-y<35){
cout<<"continue work "<<endl;
cout<<“Name:”<<name<<endl;
cout<<“TeacherNum:”<<teachernum<<endl;
return 1;
}
return 0;
}
这个const就是有问题的。
2.这个是关于拷贝函数(const &)可以使用传进来的对象调用自己的私有变量,这是一个概念问题,可以参考这个博客,讲的比较好。
https://blog.csdn.net/weixin_40539125/article/details/84205194
3.对于运算符重载问题,() [] -> = ,重载则必须使用成员函数
friend 函数其实相当于不是类的函数,所以并不能调用自己的this,
重载运算符分为友元与非友元两种。
‘=’重载:
Date Date::operator = (const Date &d){
this->year=d.year;
this->month=d.month;
return *this;
}
word& word::operator++(){ //++a
++(this->record);
return *this;
}