3
votes

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.

3
"Which did not work for me as well" How it didn't work for you specifically? You need the global operator overloaded like this: ostream& operator(ostream& out, const Device& dev). - πάντα ῥεῖ
Ok, without "<<" and not within the class? - Amit
Sorry, that was a typo: ostream& operator<<(ostream& out, const Device& dev), yes outside of the class. - πάντα ῥεῖ
I get the exact same error. ostream& operator<<(ostream& out, const Device& v) { out << "Device " << v.get_name() << " Has an ID of: " << v.get_id(); return out; } - Amit
Look at msdn.microsoft.com/en-us/library/1z2f6c2k.aspx "Overloading the << Operator for Your Own Classes". If you do that right it should work. - AAT

3 Answers

2
votes

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.

0
votes

Overloading C++ STL methods

I'm not convinced you can overload the << operator on an STL stream based on this answer.

0
votes

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;
}