插入运算符的非成员函数窗体
Non-member-function form for insertion operator
我想要一个非类成员重载的 put 运算符,它使用引用参数从汽车对象输出信息。
这是我的代码:
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;
}
我收到std::ostream& Car::operator<<(std::ostream&, Car&)' must take exactly one argument
错误
我不允许将汽车作为参数吗?
谢谢。
您说要定义一个非成员运算符。但是,您将运算符定义放在类定义中,这使得编译器将其视为成员(此运算符的成员实现必须只有一个参数,因此会出现错误消息)。如果要定义非成员运算符,请将其移出类定义或将其声明为 friend
(或两者兼而有之)
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何在函数中暂停程序,直到用户在窗体上执行某些操作
- 插入运算符的非成员函数窗体
- C++Borland Builder窗体-调用一个函数
- 在调用不同类的函数时刷新 Windows 窗体
- 从另一个函数更改窗体"visible"属性?
- 如何在 Windows 窗体中为事件函数定义使用单独的.cpp文件
- 从Windows窗体类调用PThread到Windows窗体类内的函数
- 将类类型传递给Windows窗体构造函数,并且必须将其强制转换为类指针才能使用它
- 如何在qt构造函数完成执行前显示窗体
- 在c++中从子窗体访问父函数
- 从c#调用DLL c++函数而不阻塞窗体控件
- 在.net c++中调用父窗体的子窗体函数