user12201392
user12201392

Reputation:

cout a function of a class

I have an IT homework for school but I run into a cout problem. The program should calculate complex numbers.

#include <iostream>
#include <cmath>
#include <complex>

using namespace std;

class Complex
{

public:
    Complex()
    {
        Re = 0;
        Im = 0;
    }

    Complex(float r, float i) 
    {
        Re = r;
        Im = i;
    }

    void SetRe(float n)
    {
        Re = n;
    }

    float GetRe()
    {
        return Re;
    }

    void SetIm(float n)
    {
        Im = n;
    }

    float GetIm()
    {
        return Im;
    }

    float GetR()
    {
        float r;
        float ar = pow(Re, 2);
        float br = pow(Im, 2);
        r = sqrt(ar + br);

        return r;
    }

    float GetPhi()
    {
        float phi;
        phi = atan2(Im, Re);
        phi = phi * 180 / 3.1415;
        return phi;
    }

    Complex add(Complex s2)
    {
        Complex sum(Re + s2.Re, Im + s2.Im);
        return sum;
    }

private:
    float Re;
    float Im;

};

int main()
{
    float im = 0;
    float re = 0;
    cout << " Enter Real: ";
    cin >> re;
    cout << " Enter Imag: ";
    cin >> im;
    for (int i = 0; i < 30; i++)
    {
        cout << "=";
    }
    cout << endl;

    Complex z1 = Complex(re, im);

    cout << " z1: " << z1.GetRe() << " + " << z1.GetIm() << "i" << endl;
    cout << " r: " << z1.GetR() << endl;
    cout << " Phi: " << z1.GetPhi() << endl;

    cout << " Enter Real: ";
    cin >> re;
    cout << " Enter Imag: ";
    cin >> im;

    Complex z2 = Complex(re, im);

    Complex z;

    cout << z1.add(z2); //This gives the error

    cout << " z2: " << z2.GetRe() << " + " << z2.GetIm() << "i" << endl;
    cout << " r: " << z2.GetR() << endl;
    cout << " Phi: " << z2.GetPhi() << endl;


    cout << endl;
}

I want the function z1.add(z2); to be displayed, but Xcode says:

Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream<char>') and 'Complex')

If I remove cout << z1.add(z2);, everything works fine.

Upvotes: 1

Views: 625

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

You did not define the operator << for your class Complex. So the compiler does not know how to output an object of the class.

You could define it for example the following way

std::ostream & operator <<( std::ostream &os, const Complex &c )
{
    return os << "{ " << c.GetRe() << ", " << c.GetIm() << "i }";
}

To do this the member functions GetRe and GetIm shall be declared with the qualifier const. For example

float GetRe() const
{
    return Re;
}

float GetIm() const
{
    return Im;
}

After that you can write

std::cout << z1.add(z2) << '\n'; 

Pay attention to that the member function add also should be declared like

Complex add( const Complex &s2 ) const 
{
    Complex sum(Re + s2.Re, Im + s2.Im);
    return sum;
}

Or just

Complex add( const Complex &s2 ) const 
{
    return { Re + s2.Re, Im + s2.Im };
}

Upvotes: 3

NutCracker
NutCracker

Reputation: 12263

Since the return value of your add method is Complex object, you should overload << operator.

ostream & operator << (ostream &out, Complex const& c)
{
    out << c.Re;
    out << "+i" << c.Im << endl;
    return out;
}

Upvotes: 0

Related Questions