0

I am getting trouble to output the object. I get an error in "cout << sum;" line. How can I print this Dual Number Object as D(float, float).

#include <iostream>
using namespace std;

class Dual{
    public:
        float val, eps;
        Dual(float v, float e){
            val = v;
            eps = e;
        }

        Dual operator+(Dual const &obj) {
            Dual res(0, 0);
            res.val = val + obj.val;
            res.eps = eps + obj.eps;
            return res;
        }

        void print() const { 
            cout << "Dual(" << val << ", " << eps << endl; 
        }
};

int main(){
    Dual d1(1, 2), d2(4, 5);
    Dual sum = d1 + d2;
    cout << sum;
    return 0;
}
  • 7
    Maybe you meant to call `sum.print()` ? – Ben Voigt Apr 28 '21 at 17:41
  • Otherwise you need to create your own operator <<() for your class like this example: [https://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes?view=msvc-160](https://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes?view=msvc-160) however then what is the purpose of the print() member if you are not going to use it. – drescherjm Apr 28 '21 at 17:46
  • 1
    What do you expect to happen? How does `std::cout` know how to print a `Dual`? You need to define `std::ostream& operator<<(std::ostream& o, const Dual& d)` – Pablochaches Apr 28 '21 at 17:47

2 Answers2

2

Change:

        void print() const { 
            cout << "Dual(" << val << ", " << eps << endl; 
        }

to something like this:

        friend std::ostream &operator<<(std::ostream &os, Dual const &d) { 
            os << "Dual(" << d.val << ", " << d.eps << ")"; 
            return os;
        }

... and off you go. Note that I've intentionally omitted printing a new-line as part of printing the Dual object. Oh, and when you want a new-line, just use "\n", not std::endl, which (at least IMO) was kind of a mistake and should be avoided in general.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Using a hidden `friend` function like this is far superior to an otherwise free function. Making this a hidden friend means that `cout << SomethingNotADual{}` won't consider it in overload resolution, improving performance and also meaning that GCC won't show this overload in its long error messages where it shows every "Considered this overload, but it didn't work because ...". – Justin Apr 28 '21 at 21:22
2

You have not defined an operator<< for Dual, that is why you are getting the error. You do, however, have a print() method implemented, but you are not using it.

So, you can either:

  • simply change cout << sum; to sum.print();
int main(){
    Dual d1(1, 2), d2(4, 5);
    Dual sum = d1 + d2;
    sum.print();
    return 0;
}
  • otherwise, implement operator<<:
class Dual{
    public:
        float val, eps;
        ...
};

ostream& operator<<(ostream &os, Dual const &obj) { 
    os << "Dual(" << obj.val << ", " << obj.eps << endl;
    return os;
}

If you still want to use print(), you should change it to take an ostream as input:

class Dual{
    public:
        float val, eps;

        ...

        void print(ostream &os) const { 
            os << "Dual(" << val << ", " << eps << endl; 
        }
};

ostream& operator<<(ostream &os, Dual const &obj)
{
    obj.print(os);
    return os;
}

Then, you can use either of these:

cout << sum;
sum.print(cout);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770