Thread: Operator Overloading

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by BKurosawa View Post
    How do I fix my [] operator then?
    I just said -- by declaring translateIndexToZeroBased() as a 'const' method.

  2. #17
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by BKurosawa View Post
    How do I fix my [] operator then?
    Did you look at the code I posted? Both of your [] operators work fine. And it passes the tests of the operators I fixed.
    Don't quote me on that... ...seriously

  3. #18
    Registered User
    Join Date
    Jul 2007
    Posts
    109

    Smile

    WOW!

    Brad0407, you are brilliant!

    Thank you very much for your help.

    It was pure genius.

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Also, to all the others that helped, thank you!

  5. #20
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    does anyone see a problem with my += operator from my previous posts. Ill provide the code again. Right now the problem is that it when it adds the contents of the right hand operand to the left hand operand it outputs -842150451 as the array contents.

    Code:
    IntArray& IntArray::operator+= (IntArray& add)
    {
    	CreateArray(add.size);
    	for(int k = add.low(); k<= add.high() ; k++)
    		parray[k] += add.parray[k];
    	return *this;
    }

  6. #21
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Problems:
    1. You don't check to see that the arrays are the same size.
    2. They may not have the same start and finish point so you should not go from low() to high() but from 0 to size.
    3. You shouldn't be recreating the array with CreateArray()
    4. I'm awake right now
    Don't quote me on that... ...seriously

  7. #22
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Brad0407 View Post
    Problems:
    1. You don't check to see that the arrays are the same size.
    2. They may not have the same start and finish point so you should not go from low() to high() but from 0 to size.
    3. You shouldn't be recreating the array with CreateArray()
    4. I'm awake right now
    do I just completely leave out CreateArray()? Thanks.

  8. #23
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Brad0407 View Post
    Problems:
    1. You don't check to see that the arrays are the same size.
    2. They may not have the same start and finish point so you should not go from low() to high() but from 0 to size.
    3. You shouldn't be recreating the array with CreateArray()
    4. I'm awake right now
    Nevermind, it works. Thanks again!

Popular pages Recent additions subscribe to a feed