0

I'm writing a c++ linked list using ostream operator, and I am stuck. What have I done wrong?

// Train class
class Car{
    public:
        void print(ostream&) const;
        friend std::ostream &operator<<(std::ostream&, const Car&);
};

void Car::print(ostream& out) const{
        out << "OK" << endl;
 }

ostream& operator<<(ostream& os, const Car& car){
    os << car->print(os);
    return os;
}

error: base operand of ‘->’ has non-pointer type ‘const Car’
make: ***[Car.o] Error 1

Things I have tried:
1) os << car->print(*os);
2) os << car.print(os); // it gets worse

  • car.print(os) instead of car->print(os) – Abdul Ahad Apr 08 '18 at 23:59
  • "It gets worse" because you have another error. Change `os << car->print(os);` to `car->print(os);`, and next time start with simpler code, then build up. – Beta Apr 09 '18 at 00:02
  • **error:** no match for ‘operator<<’ in ‘os << ((const Car*)car)->Car::print(((std::ostream&)((std::ostream*)os)))’ – Billion Boi Apr 09 '18 at 00:03
  • @Beta, thanks for your comment. That does not work either. – Billion Boi Apr 09 '18 at 00:05
  • Learn about the [difference between a reference and a pointer](https://stackoverflow.com/q/57483/9254539), specifically, the fact that a reference doesn't need to be dereferenced. – eesiraed Apr 09 '18 at 00:41
  • Sorry, my mistake, I meant `os << car.print(os);` to `car.print(os);` – Beta Apr 09 '18 at 00:42

1 Answers1

4

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;
    // ...
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • thank you for your reply. Yes, it was my fault that mixed up my functions. actually, print() was declared. As you mentioned that void function cannot accept return type as para, how would I go about it? – Billion Boi Apr 09 '18 at 00:17