Things I have tried:
1) os << car->print(*os);
base operand of ‘->’ has non-pointer type ‘const Car’
The error should be quite clear. You've applied the indirecting member access operator ->
on a non-pointer (of a type that doesn't overload that operator). That's something that you can not do. Presumably, you intended to call Car::print
instead. That can be done using the regular member access operator .
ostream& os
print(*os)
This is wrong. There is no indirection operator for ostream
. Since print
accepts a ostream&
as an argument, presumably you intended to pass os
to the function instead.
void Car::print
Car::print
returns void
i.e. it doesn't return any value. Yet, you insert the return value into the stream. You cannot insert void
into a stream. If you intend to return something from the function, then change the return type to whatever you intended to insert into the stream. Or, if you only intend to insert things into the stream within the function, then simply don't insert the return value of the function:
When we fix all three of these things, we end up with
car.print(os);
Finally, Car::print
isn't declared in the definition of Car
. All member functions must be declared in the class definition. The declaration should look like this:
class Car{
public:
void print(ostream& out) const;
// ...
}