Thread: Operator Overloading

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

    Operator Overloading

    Ok so i've read about doing this. I have this so far in my header file.
    Code:
    friend BigInt operator +(const BigInt&, const BigInt&);
    friend bool operator <(const BigInt&, const BigInt&);
    friend bool operator <=(const BigInt&, const BigInt&);
    friend bool operator >(const BigInt&, const BigInt&);
    friend bool operator >=(const BigInt&, const BigInt&);
    friend std::ostream& operator <<(std::ostream&, const BigInt&);
    my question is how do implement these, like the head syntax line for the > operator is it
    Code:
    bool operator::BigInt <(const BigInt& a, const BigInt& b)
    Last edited by Salem; 05-02-2007 at 04:25 PM. Reason: fixed tags

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Your tags should end like "[/code]".

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    ok i forgot this time but that has no reference to my question. also i did the < operator and i get 2 errors and i don't know why

    Code:
     
    204 bool BigInt::operator <(const BigInt& a, const BigInt& b)
    205 {   
    206     if(a.positive && !b.positive)
    207     {   
    208         return false;
    209     }   
    210     else if(!a.positive && b.positive)
    211     {   
    212         return true;
    213     }   
    214     else if(a.positive && b.positive && a.numDigits > b.numDigits)
    215     {
    216         return false;
    217     }
    218     else if(!a.positive && !b.positive && a.numDigits < b.numDigits)
    219     {
    220         return true;
    221     }
    222 }
    error at compile time

    Code:
    g++ -c  BigInt.cpp
    BigInt.cpp:204: error: ‘bool BigInt::operator<(const BigInt&, const BigInt&)’ must take exactly one argument
    BigInt.cpp:204: error: no ‘bool BigInt::operator<(const BigInt&, const BigInt&)’ member function declared in class ‘BigInt’
    make: *** [BigInt.o] Error 1

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    These are not member functions, but regular functions. Therefore you shouldn't define them as member functions:
    Code:
    bool BigInt::operator <(const BigInt& a, const BigInt& b)

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    thank you.

Popular pages Recent additions subscribe to a feed