【发布时间】:2018-04-08 23:57:37
【问题描述】:
我正在使用 ostream 运算符编写一个 c++ 链表,但我被卡住了。 我做错了什么?
// 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;
}
错误:“->”的基本操作数具有非指针类型“const Car”
make: ***[Car.o] 错误 1
我尝试过的事情:
1) os print(*os);
2) 操作系统
【问题讨论】:
-
car.print(os) 而不是 car->print(os)
-
“情况变得更糟”,因为您还有另一个错误。把
os << car->print(os);改成car->print(os);,下次从更简单的代码开始,然后编译。 -
错误: 'os Car::print(((std::) 中的 'operator
-
@Beta,感谢您的评论。那也不行。
-
了解difference between a reference and a pointer,特别是不需要取消引用的事实。
标签: c++ class operator-overloading ostream