C ++ ostream重载不起作用(C++ ostream overloading not working)

编程入门 行业动态 更新时间:2024-10-26 03:39:46
C ++ ostream重载不起作用(C++ ostream overloading not working)

编辑:

经过一些评论,现在这是我的代码,遵循此链接。(更好,但我仍然有错误)

离婚:

ostream& operator<<(ostream& out, Device& v) { out << "Device " << v.get_name() << " Has an ID of: " << v.get_id(); return out; }

内部设备类:

friend ostream& operator<<(ostream& os, const Device& v);

我的调用:(设备类型为Node,val返回设备)

cout << device->val << endl;

我的错误:

错误LNK2019未解析的外部符号“class std :: basic_ostream> std :: char_traits>&__ cdecl operator <<(class std :: basic_ostream>&,class Device const&)”(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ ABVDevice @@@ Z)在函数“void __cdecl print_devices(class Node *)”中引用(?print_devices @@ YAXPAV?$ Node @ VDevice @@@@@ Z)

原版的:

我被告知,重载运算符是这样的:

ostream& Device::operator<<(ostream &out) { out << "Device " << this->name << " Has an ID of: " << this->id; return out; }

但是在尝试使用这种重载时 - (设备是类型设备)

cout << device << endl;

它标志着阅读并说 -

错误C2679二进制'<<':找不到哪个运算符带有'Device'类型的右手操作数(或者没有可接受的转换)

为什么我会收到此错误,如何解决? 我在网上看了,但找不到一个在课堂上工作的方法,只有这个:

朋友ostream&operator <<(ostream&out,Point&cPoint);

哪个对我也不起作用。

Edit:

After some comments, this is my code now, following THIS link.(Better, but I still have an error)

Out of everyhing:

ostream& operator<<(ostream& out, Device& v) { out << "Device " << v.get_name() << " Has an ID of: " << v.get_id(); return out; }

Inside Device class:

friend ostream& operator<<(ostream& os, const Device& v);

My call: (device is of type Node, and val returns the device)

cout << device->val << endl;

My error:

Error LNK2019 unresolved external symbol "class std::basic_ostream > std::char_traits > & __cdecl operator<<(class std::basic_ostream > &,class Device const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVDevice@@@Z) referenced in function "void __cdecl print_devices(class Node *)" (?print_devices@@YAXPAV?$Node@VDevice@@@@@Z)

Original:

I was taught that overloading an operator is made like this:

ostream& Device::operator<<(ostream &out) { out << "Device " << this->name << " Has an ID of: " << this->id; return out; }

But when trying to use this overloading - (device is type Device)

cout << device << endl;

It marks in read and says -

Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'Device' (or there is no acceptable conversion)

Why do I get this error, and how can I fix it? I looked online, but could not find a method that works inside the class, only this:

friend ostream& operator<< (ostream &out, Point &cPoint);

Which did not work for me as well.

最满意答案

您在Device类中声明的内容是

friend ostream& operator<<(ostream& os, const Device& v);

但你提供的实现是什么

ostream& operator<<(ostream& out, Device& v) { out << "Device " << v.get_name() << " Has an ID of: " << v.get_id(); return out; }

不是一回事 ! 你告诉编译器有一个friend函数,它引用了一个ostream和一个对const的const引用 - 但是你提供的const错过了Device前面的const 。

What you have declared inside your Device class is

friend ostream& operator<<(ostream& os, const Device& v);

but what you have provided the implementation for is

ostream& operator<<(ostream& out, Device& v) { out << "Device " << v.get_name() << " Has an ID of: " << v.get_id(); return out; }

which is not the same thing! You told the compiler there is a friend function which takes a reference to an ostream and a const reference to a Device - but the function you have provided misses the const in front of Device.

更多推荐

本文发布于:2023-08-04 20:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1421391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   ostream   working   overloading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!