Thread: Operator Overload problem

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    Operator Overload problem

    I recently learned operator overloading from this book I am reading and decided to test it out with some code. I can't find any errors but I get a weird output... I can't really explain. Well here's the code.

    Code:
    #include <iostream>
    using namespace std;
    
    class ints {
          int x, y, z;
       public:
          ints() { x = y = z = 0; }
          ints(int a, int b, int c) { x = a; b = y; z = c; }
          ints operator+(ints op2);
          ints operator=(ints op2);
          void show();
    };
    
    ints ints::operator+(ints op2)
    {
          ints temp;
    
          temp.x = x + op2.x;
          temp.y = y + op2.y;
          temp.z = z + op2.z;
    
          return temp;
    }
    
    ints ints::operator=(ints op2)
    {
          x = op2.x;
          y = op2.y;
          z = op2.z;
    
          return *this;
    }
    
    void ints::show()
    {
          cout << x << ", ";
          cout << y << ", ";
          cout << z << '\n';
    }
    
    int main()
    {
          ints a(1, 2, 3), b(10, 11, 12), c;
    
          a.show();
          b.show();
          c = a + b;
          c.show();
    
          return 0;
    }
    The output I get is:
    1, 2009376667, 3
    10, -1, 12
    11, 2009376666, 15

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The problem isn't in your overloaded operators, it's in your constructor:
    Code:
    ints(int a, int b, int c) { x = a; b = y; z = c; }
    The code in red should be y = b. You're assigning to the parameter, not the class member. You want it the other way around.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    gah I knew it would be something simple like that. Thanks man I've been looking at it forever trying to figure out what the heck would cause such a weird error.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've been looking at it forever trying to figure out what the heck would cause such a weird error.
    If you get strange numbers like that you can be reasonably sure that the problem is an uninitialized variable.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM