Stefan Paval
Stefan Paval

Reputation: 46

C++ overload << and unary minus

I have a strange error (no match for 'operator<<' in std::cout << BigReal::operator-()() ) when doing this:

cout<<(-D);

where << is overloaded as following:

ostream & operator<<( ostream &c, BigReal &n )
{
    c << n.nume << "=" << "[" << n.nr << "]  ";
    return c;
}

and unary - as:

BigReal BigReal::operator-( void )
{
    float negativ = atof( nr );

    char buff[ 1000 ];
    sprintf( buff, "%f", -negativ );

    //strcpy( nr, buff );
    BigReal Rez(buff, "Nr.");
    return Rez;
}

and D is a BigReal.

Upvotes: 0

Views: 121

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

Expression -D creates a temporary object. A temporary object may be bound to const reference. So change operator << the following way

ostream & operator<<( ostream &c, const BigReal &n );

Upvotes: 2

Related Questions