Thread: Overloading operators issue

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Overloading operators issue

    Code:
    // vectors: overloading operators example
    #include <iostream>
    using namespace std;
    
    class CVector {
      public:
        int x,y;
        CVector () {};
        CVector (int,int);
        CVector operator + (CVector);
    };
    
    CVector::CVector (int a, int b) {
      x = a;
      y = b;
    }
    
    CVector CVector::operator+ (CVector param) {
      CVector temp;
      temp.x = x + param.x;
      temp.y = y + param.y;
      return (temp);
    }
    
    int main () {
      CVector a (3,1);
      CVector b (1,2);
      CVector c;
      c = a + b;
      cout << c.x << "," << c.y;
      return 0;
    }
    my problem is with this line:

    Code:
    CVector CVector::operator+ (CVector param) {
    why does CVector class name have to be written twice otherwise the compiler will report error? In this same code, other class functions has been defined in the global scope, with the class name CVector being written only once. This is where I'm stuck.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    CVector CVector::operator+ (CVector param) {
    When a member function's definition occurs outside of its class declaration, then you have to qualify the member function's name with the class name. Since you also return a CVector object this means writing the class name twice.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There are a lot of things I would improve with that example. Here is my first shot at improving it:
    Code:
    // vectors: overloading operators example
    #include <iostream>
    using namespace std;
    
    class CVector {
        int x,y; // Note that these are now private. That is certainly optional though
      public:
        CVector() {} // No semicolon needed here
        CVector(int x, int y) : x(x), y(y) {} // Constructor initialisation list. Learn it, love it.
        // Putting these two inside the class means no need to write CVector:: any more
        // Prefer the two parameter friend version of this operator
        friend CVector operator + (const CVector& lhs, const CVector& rhs) { // Pass by const-reference
            return CVector(lhs.x + rhs.x, lhs.y + rhs.y);
        }
        // This overload tells the class how to print itself instead of main having to know how!
        friend ostream& operator << (ostream &os, const CVector &v) { // Const-correctness is always good
            return os << v.x << "," << v.y;
        }
    };
    
    int main () {
        CVector a(3, 1);
        CVector b(1, 2);
        CVector c = a + b;
        cout << c; // booyah!
        return 0;
    }
    Warning: Untested code
    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
    Jul 2010
    Posts
    26
    Quote Originally Posted by dhuan View Post
    my problem is with this line:

    Code:
    CVector CVector::operator+ (CVector param) {
    why does CVector class name have to be written twice otherwise the compiler will report error?
    The first CVector here is the return type of the function; almost all functions have a return type - a function might return int, or float or something else. This one returns an object of type CVector.

    The second CVector (before ::) is for scope resolution. Since you are defining the class function outside the body of the class, you need to tell the compiler that this function, named operator+, belongs to the CVector class, and not some other class.

    In this same code, other class functions has been defined in the global scope, with the class name CVector being written only once. This is where I'm stuck
    The other class functions, CVector() and CVector(int, int), are constructors and it is the property of constructors that they do not have a return type - not even void; so the only CVector there is for scope resolution.

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. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:24 PM