-1

I've been making a class called Complex for storing and working with complex numbers. I'm having issues where my overloaded + and * functions return an obscenely large number, which I can only presume has something to do with pointers, but I'm very new to using them. I've copied the output here:

c1: 1 - i5
c2: -4 + i3
c1 + c2: -9.25596e+061 - i9.255
c1 * c2: -9.25596e+061 - i9.255
Press any key to continue . . .

Here is the class architecture:

// Complex.h
#include <iostream>
#include <cmath>    
using namespace std;

class Complex
{
    friend std::ostream &operator<<(std::ostream &, const Complex &);
    friend std::istream &operator>>(std::istream &, Complex &);
    friend Complex &operator+(const Complex &, const Complex &);
    friend Complex &operator+(const Complex &, const double &);
    friend Complex &operator+(const double &, const Complex &);
    friend Complex &operator*(const Complex &, const Complex &);
    friend Complex &operator*(const Complex &, const double &);
    friend Complex &operator*(const double &, const Complex &);
public:
    explicit Complex(double re=0, double im=0);
    void setReal(double re);
    void setImag(double im);
    double &getReal();
    double &getImag();
    ~Complex();
private:
    double real;
    double imag;
};

And here I have the relevant portions of my program:

// Complex.cpp
#include "Complex.h"

ostream &operator<<(ostream &output, const Complex &num)
{
    output << num.real << (num.imag >= 0 ? " + i" : " - i") << abs(num.imag);
    return output;
}

Complex &operator+(const Complex &num1, const Complex &num2)
{
    Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
    return sum;
}

Complex &operator*(const Complex &num1, const Complex &num2)
{
    Complex prod(num1.real * num2.real - num1.imag * num2.imag, num1.real * num2.imag + num1.imag * num2.real); // Create new complex by expansion
    return prod;
}

Complex::Complex(double re, double im)
{
    real = re;
    imag = im;
}

int main() {
    Complex c1(1.0, -5.0);
    Complex c2(-4.0, 3.0);
    cout << "c1: " << c1 << endl;
    cout << "c2: " << c2 << endl;
    cout << "c1 + c2: " << c1 + c2 << endl;
    cout << "c1 * c2: " << c1 * c2 << endl;
    system("pause");
}

I feel bad bothering you guys about this. Do you possibly know of a site that does a good job of explaining concepts for C++ like pointers, least-privilege, polymorphism, inheritance, etc?

Sorry for asking basic questions on here, but it's hard to find resources that explain this stuff clearly.

Keegs
  • 117
  • 5
  • buy (down...d) yourself a good C++ book http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Although most of these books deserved to be bought. – vsoftco Apr 05 '15 at 01:04

2 Answers2

1

The problem is that most of your overloaded operators return a reference to a local var, which is destroyed when the operator function returns, leaving you with a dangling reference. Try changing your operators to return a Complex rather than a Complex &:

Complex operator+(const Complex &num1, const Complex &num2)
{
    Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
    return sum;
}
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

Wrong return type:

Complex &operator+(const Complex &num1, const Complex &num2)
^^^^^^^^^

You're creating a temporary Complex and returning it by reference, and then it gets destroyed so you have a dangling reference. Since operator+ is constructing a new object and returning it, it should return it by value... so the signature should be:

Complex operator+(Complex const&, Complex const& );
Barry
  • 286,269
  • 29
  • 621
  • 977