简体   繁体   English

C ++ ostream重载不起作用

[英]C++ ostream overloading not working

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) 我的调用:(设备类型为Node,val返回设备)

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) 错误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)

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) 错误C2679二进制'<<':找不到哪个操作符采用了'Device'类型的右操作数(或者没有可接受的转换)

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); 朋友ostream&operator <<(ostream&out,Point&cPoint);

Which did not work for me as well. 哪个对我也不起作用。

What you have declared inside your Device class is 您在Device类中声明的内容是

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 . 你告诉编译器有一个friend函数,它接受一个参考ostream常量引用一个Device -但你所提供的功能,忽略了const前面Device

Overloading C++ STL methods 重载C ++ STL方法

I'm not convinced you can overload the << operator on an STL stream based on this answer. 我不相信你可以根据这个答案重载STL流上的<<运算符。

The error you've posted is about the compiler not finding the function implementation. 您发布的错误是关于编译器没有找到函数实现。

#include <iostream>

struct MyType
{
    int data{1};

};

std::ostream& operator<< (std::ostream& out, const MyType& t)
{
    out << t.data;
    return out;
}

int main()
{
    MyType t;

    std::cout << t << std::endl;

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM