Overloading operators...

This is a discussion on Overloading operators... within the C++ Programming forums, part of the General Programming Boards category; Here's my code... Code: // Operators inline Vector operator- (Vector v) const { return Vector(-v.X(), -v.Y()); } inline bool operator== ...

  1. #1
    Unregistered
    Guest

    Unhappy Overloading operators...

    Here's my code...

    Code:
    // Operators
    inline Vector operator- (Vector v) const		   { return Vector(-v.X(), -v.Y());		          }
    inline bool operator== (Vector v, Vector u) const  { return v.X() == u.X() && v.Y() == u.Y();	  }
    inline bool operator!= (Vector v, Vector u) const  { return !(v == u);			                  }
    inline Vector operator+ (Vector v, Vector u) const { return Vector(v.X() + u.X(), v.Y() + u.Y()); }
    inline Vector operator- (Vector v, Vector u) const { return Vector(v.X() - u.X(), v.Y() - u.Y()); }
    inline Vector operator* (Vector v, int in) const   { return Vector(v.X() * in, v.Y() * in);       }
    inline Vector operator/ (Vector v, int in) const   { return Vector(v.X() / in, v.Y() / in);       }
    Here's the error: c:\my documents\pong16\vector.h(33) : error C2270: '-' : modifiers not allowed on nonmember functions

    This error repeats for all the operators. I should note, this overloading is taking place outside of the class definition, but in the same file.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It's because only member functions can have the const modifier. You'll either have to remove the modifier or move the functions inside the class declaration.
    zen

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It's the const, this compiles errorless with MSVC. const only has meaning after the function definition with classes, and it means that no data (other than mutable stuff, but that's an exception you probably don't have to worry about) is changed during the function call. If it's a non-member function, that const just confuses the compiler, because it is essentially meaningless. What data is there to be modified? const is specified per parameter, so obviously not them.

    class test {
    public:
    test(int a) { blah = a;}
    int blah;
    };

    test operator - (test other)
    /* const is meaningless here, it's not a member function, so there is no implicit this
    object that could be modified*/ {
    other.blah = -other.blah;
    return other;
    }

    int main() {
    test a(4);
    test b = -a;
    return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Dah, I go and test code, write a whole paragraph, only to respond 15 minutes late with the correct answer .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Unregistered
    Guest
    Thanks guys. It works now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 11:27 AM
  3. Replies: 16
    Last Post: 10-27-2007, 01:42 PM
  4. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:17 AM
  5. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 02:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21