Thread: operator overloading errors

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    operator overloading errors

    Im trying to over load the / + * - operators, but cannot find a descent tutorial on doing so. Yes I've read up on overloading, and it is probably not a good idea till I know exactly what i'm doing, but I have to learn sometime. I'm not sure on who to use the functions in the program after I have written the implementation. How does the ostream work after overloading it? The same as before, or is there a new way of setting it up? Is what I have for the main so far correct?

    Code:
    #include <iostream>
    #include <cstdlib>
    
    bool division = false;
    bool subtract = false;
    bool add = false;
    bool mult = false;
    
    using namespace std;
    
    class COMPLEX;
    
    ostream operator<<(ostream&,const COMPLEX&);
    istream operator>>(istream&, COMPLEX&);
    
    class COMPLEX{
    
        private:
    
            int real, imaginary; //initial values
            int realfin,imfin;
            
        public:
    
            COMPLEX(); //defines default constructor
            ~COMPLEX(); //destructor
    
            COMPLEX operator/(COMPLEX&);
            COMPLEX operator+(COMPLEX&);
            COMPLEX operator*(COMPLEX&);
            COMPLEX operator-(COMPLEX&);
    
        friend ostream operator>>(istream&,const COMPLEX&);
        friend istream operator>>(istream&, COMPLEX&);
    
    }comp1,comp2,final; //define the classes to be used
    
    
    
    // This is the main .cpp file, the other implementations are above this
    
    istream operator>>(istream &strm, COMPLEX &obj){
        cout<<"enter a real and imaginary number\n";
        strm>> real >> imaginary;
        
        cout<<"enter a second real and imaginary number\n";
        strm>> obj.real >> obj.imaginary;
        
        return strm;
    }
    
    
    int main(){
    
        int real, imag;
        cin>>comp1;
    
        final = comp1 * comp2;
        cout<<final;
        final = comp1 / comp2;
        cout<<final;
        final = comp1 + comp2;
        cout<<final;
        final = comp1 - comp2;
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    istream& operator>>(istream &strm, COMPLEX &obj){
        cout<<"enter a real and imaginary number\n";
        strm>> real >> imaginary;
        
        cout<<"enter a second real and imaginary number\n";
        strm>> obj.real >> obj.imaginary;
        
        return strm;
    }
    Why do you expect this overload to read two complex numbers? And what do you expect to do with real and imaginary?
    Also, you'll need to return the streams as a reference.

    I suggest a usage such as:
    Code:
    int main(){
    
        //int real, imag; //what are these for?
    
    //no global objects
        COMPLEX comp1, comp2, final;
        cin >> comp1 >> comp2; //read some value into both
    
        final = comp1 * comp2;
        cout<<final;
        final = comp1 / comp2;
        cout<<final;
        final = comp1 + comp2;
        cout<<final;
        final = comp1 - comp2;
        cout << final;
    }
    That is, you have some general problems that have nothing to do with operator overloading.

    As to the math operators: they should not change neither the lefthand nor the righthand value, so these methods should be const and their argument should be a const reference.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
        friend ostream& operator<<(istream&,const COMPLEX&);
        friend istream& operator>>(istream&, COMPLEX&);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    thanks guys, i think i need to do a lot more reading on this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  4. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM