Pregunta

I want to have a non-class member overloaded put operator which uses reference parameters to output the information from a car object.

Here is my code:

ostream& operator<<(ostream& os, Car& p)
{
         os << "For a car make " << p.get_make() << ", " << p.get_year()<< ", the price is $" << p.get_price()  << endl;
         return os;
}

I get a std::ostream& Car::operator<<(std::ostream&, Car&)' must take exactly one argument error

Am I not allowed to have a Car as a parameter?

Thanks.

¿Fue útil?

Solución

You said you wanted to define a non-member operator. Yet you placed your operator definition inside class definition, which made the compiler to treat it as member (member implementation of this operator must have only one parameter, hence the error message). If you want to define a non-member operator, either move it outside of class definition or declare it as friend (or both)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top