编辑:
经过一些评论,现在这是我的代码,遵循此链接。(更好,但我仍然有错误)
离婚:
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.
更多推荐
发布评论