-
Overloaded operators
This is a program that will process complex numbers (real and imaginary), but I'm getting an Internal Compiler Error:
Code:
#include <iostream>
using namespace std;
class Complex
{
double real, img;
public:
Complex( double realval = 0.0, double imagval = 0.0 ) : real(realval), img(imagval)
{
} // default contructor
Complex( const Complex &right )
{
real = right.real;
img = right.img;
} // copy constructor
friend Complex operator+( const Complex & left, const Complex &right );
friend ostream &operator<<( ostream &output, const Complex &right );
};
Complex operator+(const Complex &left, const Complex &right )
{
return Complex(left.real+right.real, left.img+right.img);
}
ostream &operator<<( ostream &output, const Complex &right )
{
return output << "(" << right.real << ") + (" << right.img << ")i";
}
int main()
{
Complex A(2.5,-2.2), B(5.0,1.0), C;
C = A + B;
cout << "A is first complex number: " << A << endl;
cout << "B is second complex number: " << B << endl;
cout << "C is A + B: " << C << endl;
return 0;
}
-
Works fine using g++ 3.3.5
Kurt
-
which compiler are you using? I don't have any problems with VC 2003.
-
Works fine in VC++ 6 & 7.1
-
I thought that looked familiar. Wow, almost a year ago and I still recognized it (even thought it's got a few cosmetic changes).
-
Your code must be going around! :p Hmm, I'm using MS Visual C++ that came with the 4th Edition book.
-
The 4th edition of what book? When you go to Help->About... which version of the development environment is shown?