0
votes

I am calculating how long one person can get sunburn with screen protection. As I like to use different approach to do things, I created a class called 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;
    }
};

I get this error: passing 'const SunscreenSPF' as 'this' argument discards qualifiers [-fpermissive]|

How can I fix this?

1
You need to return os from the operator<< function, else you'll encounter a warning. - Zoso

1 Answers

1
votes

In the function friend ostream& operator<<(ostream &os, const SunscreenSPF &data), data has a const qualifier to it. But when calculateTime() is invoked, this member method doesn't provide the guarantee of not modifying the data object. Add a const qualifier to your calculateTime() function as

int calculateTime () const
{
    return SPF*time;
}