【发布时间】:2021-03-23 04:40:04
【问题描述】:
我正在计算一个人在使用屏幕保护膜的情况下可以晒伤多长时间。由于我喜欢使用不同的方法来做事,我创建了一个名为 SunscreenSPF 的类。
class SunscreenSPF
{
private:
string skinColor;
int SPF;
int time;
public:
SunscreenSPF(string skinColor, int SPF, int time)
{
this->skinColor=skinColor;
this->SPF=SPF;
this->time=time;
}
int calculateTime ()
{
return SPF*time;
}
friend ostream& operator<<(ostream &os, const SunscreenSPF &data)
{
os<<"Skin Color = "<<data.skinColor<<endl
<<"Time outdoors = "<<data.time<<endl
<<"SPF Level = "<<data.SPF<<endl
<<"Time to get sunburn with protection = "<<data.calculateTime()<<endl;
}
};
我收到此错误:将“const SunscreenSPF”作为“this”参数传递会丢弃限定符 [-fpermissive]|
我该如何解决这个问题?
【问题讨论】:
-
你需要从
operator<<函数返回os,否则你会遇到警告。