0

I have this simple program and when i try to cout << 75.0_stC ; i have multiple errors and i don't know why.This things only happen when i pass my temperature object via reference.

class temperature
{
    public:
        long double degrees;
        temperature(long double c): degrees{c}{}
        long double show()const {return degrees;}

};
temperature operator"" _stC(long double t){
    return temperature(t);
}
ostream & operator<<(ostream &ekran, temperature &t)
{
    ekran << t.show();
    return ekran;
}
varkus
  • 21
  • 4
  • Pass `temperature` as `const&`: https://godbolt.org/z/Pzzbfj7zE. – rturrado Jul 03 '22 at 16:11
  • `ostream&` parameter makes sense, because the stream is modified (*mutated*) by the operation. `temperature&` parameter does not make sense; why is `cout << t` **modifying** the temperature parameter? That is what the non-const *reference* tells in the signature. – Eljay Jul 03 '22 at 17:05

1 Answers1

2

You likely need to take a const reference to the argument you'd like to print:

ostream & operator<<(ostream &ekran, const temperature &t)

Temporary won't bind to the non-const reference argument.

lorro
  • 10,687
  • 23
  • 36