Overload operator

This is a discussion on Overload operator within the C++ Programming forums, part of the General Programming Boards category; Can somebody gimme a hand with this. I really have no clue why this thing is throwin errors. I don't ...

Sorry man I was just getting frustrated and pulling things on and off and forgot to put it back on. Our entire class is stumped on this assignment. wish I had something more exciting for you but its just me...the student.

OK, so then when you implement the function, you should declare is with the same arguments.

ie.... prototype:

Code:

IntArray IntArray::operator+(IntArray& rhs)

Implemented function:

Code:

IntArray IntArray::operator+(IntArray rhs)

If you think these are the same, you are terribly mistaken. One is taking in a reference to an IntArray, and one is taking in a copy of an IntArray. You should prefer the former, and declaring it as const is not a bad idea if you don't have to alter it.

I realize that just made a mistake when sending it. So your suggestion is for a implementation with no reference just passing in a copy and I would probably declare as a const as I don't need to change it. On other operators such as += I would need the reference. I get that...that version..the latter is the one I got the least errors on but the problem lay in my for loop to add each of the two values at a given index together and assign to the temp array at the same relative index. Just a side note I have an overloaded [] in my code as well that simply checks for proper range as well. And I'm just trying to wrap my head around the concept.....don't or would prefer no code as I like to do that on my own.....

Ok I misunderstood you. I just broke out the book and class notes to 'bone up' on my references. I still am not sure why I would be getting an error on something as straightforward as that for loop. And I believe our professor monitors this site so if someone steals my stuff they'll get busted. I get annoyed, probably just as much as you guys that know what you're doing, when people just say I have this problem and can you help. Or when the sorta do a bit on the assignment and send the code in and then someone finishes it for them.....meanwhile I'm pulling my hair out (even though I have none) and spending 30 hours on each project.

But it didn't like that either. I have 4 different constructors for IntArray...one that takes no parameters, one that takes 1 param 0 to that int, one that takes two size1 to size 2, and one that takes another IntArray object.

I'm about as confused as at anytime.... I was trying to use the this pointer to get the side of the lhs but it didn't like that either so I started using two parameters....althought the call looks like this:

Code:

IntArray c = a + b;

I'll just keep pluggin along hopefully it'll wack me in the face like it usually does.

Often operator+ is implemented as a non-member function, but it can be implemented as a member function as well. It looks like you are implementing it as a member function. In that case, the best prototype would be:

Code:

IntArray operator+(const IntArray& rhs) const;

Have you implemented operator+=? If so, you can just use that to do all the your work in your operator+. Create a temporary IntArray from *this, then use += to add rhs to the temp. Then return the temp and you are done.

If you continue with your current setup. Just take each line of code separately and figure out what it is doing. For IntArray temp(someSize), you are creating an object of type IntArray and passing an integer size to the constructor. Do you have a constructor that takes a size? Is that what you were trying to do? If so, and it gives you an error, then post the relevant code and someone should be able to help you with it.

>> cannot convert 'this' pointer from 'const IntArray' to 'IntArray &'
This error occurs when you try to change the this IntArray. As you know, you don't want to change a in c = a + b, and that error is telling you that you are changing a. In reality, you might not actually be changing it, but you might be calling a function that doesn't guarantee that it won't change. You should have two operator[] functions that look like this:

Code:

const int& operator[](int index) const;
int& operator[](int index);

The one that is const will be used because it guarantees it won't change a. Also, make sure your other functions are const that should be const (like size(), low() and high()).

There are several other errors in that code, like this[j] won't work because this is a pointer (use (*this)[j]). Also, IntArray temp = new IntArray(rhs.size()); is wrong because temp is not a pointer. You could just use IntArray temp(rhs.size());

Finally, as anon says you can use operator+= in your implementation of operator+ without changing a. Do this by making a temporary copy of a and calling operator+= with that.

Then you don't have to worry about calling the overloaded [] operator. If you do it this way you would just use i = 0 since each array is the same size and you're spanning from 0 to 1 less than the size.