Thread: Overloaded operators

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    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;
    }
    THE redheaded stepchild.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Works fine using g++ 3.3.5
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    which compiler are you using? I don't have any problems with VC 2003.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Works fine in VC++ 6 & 7.1

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I thought that looked familiar. Wow, almost a year ago and I still recognized it (even thought it's got a few cosmetic changes).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Your code must be going around! Hmm, I'm using MS Visual C++ that came with the 4th Edition book.
    THE redheaded stepchild.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The 4th edition of what book? When you go to Help->About... which version of the development environment is shown?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iostream overloaded operators
    By peckitt99 in forum C++ Programming
    Replies: 1
    Last Post: 08-10-2007, 05:32 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM
  4. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM
  5. help with overloaded operators
    By doleman19 in forum C++ Programming
    Replies: 23
    Last Post: 10-23-2001, 02:40 AM