简体   繁体   中英

Can't reliably add two int arrays together when overloading sum operator on objects?

I'm trying to use the operator+ function to add two objects together that contain int arrays. I am getting unreliable answers and I'm not sure why. Why are some of the members of my combined array getting strange int values?

Here is the function:

IntArray operator+(const IntArray& in1, const IntArray& in2){
    IntArray temp;

    for (int i = in1.low(); i <= in2.high(); i++){
        temp.iArray[i] = in1.iArray[i] + in2.iArray[i];
    }

    return temp;
}

and here is the output I am getting:

Sum of two arrays assigned to third array: IntArray a(1, 5);
                                           IntArray b(4, 8);
                                           IntArray c = a + b;

a[1] = 10 a[2] = 20 a[3] = 30 a[4] = 40 a[5] = 50` 
b[4] = 40 b[5] = 50 b[6] = 60 b[7] = 70 b[8] = 80`
Press any key to continue.

c[0] = 0 c[1] = 32777 c[2] = 100 c[3] = 38 c[4] = 80 c[5] = 100 c[6] = 60 c[7] = 74 c[8] = 80 c[9] = 32767 
Press any key to continue.

Am I missing something?

Edit: Adding the code for IntArray

class IntArray{
private:
    int *arrPtr;
    int iArray[SIZE];
    int arrLower, arrUpper;
    int size;
    string name;

public:
    IntArray();
    IntArray(int range);
    IntArray(int lower, int upper);
    IntArray(const IntArray& input);
    int high() const;
    int low() const;
    int compare(int in1, int in2) const;
    int operator==(const IntArray& in);  
    int operator!=(const IntArray& in);
    void setName(string input);
    IntArray& operator=(const IntArray& in);
    int& operator[] (int size)             {  return iArray[size];  }
    IntArray& operator+=( const IntArray& );
    friend IntArray operator+( const IntArray& in1, const IntArray& in2 );
    friend ostream& operator<<(ostream& os, const IntArray& i);



};

and the constructor:

IntArray::IntArray(int lower, int upper){
    arrLower = lower;
    arrUpper = upper;
    // Creates array size
    size = arrUpper - arrLower + 1;
    operator[](size);

}

Here's a link to the whole program:

https://gist.github.com/anonymous/fd4b8a5e1d1ac4d0982a

While you haven't presented your implementation for IntArray , from the results, it looks like the culprit is your for loop. I am guessing your IntArray doesn't initialize values outside its low and high bounds. However, your for loop assumes that all values with index >= in1.low() make sense for in2 . However, b[0] to b[3] would be uninitialized garbage. That is why c[0] to c[3] make no sense. An easy solution would be to change the implementation of operator[] to return an appropriate value(likely 0 ) when the index is out of bounds.

EDIT : Looks like you want operator[] to be non-const , making it more natural. So, you could simply handle the out of bound cases appropriately in operator+ . Alternatively, provide a const at function which does range checking and returns a dummy value if it is out of bounds.

Your addition operator adds array elements with indices from in1.low() to in2.high() , ie in your specific example this would be from 1 to 8. But you never initialize the values outside the respective range of either array. In each array values outside the range contain garbage. Hence the garbage in the results.

For example, in1.iArray[1] contains a meaningful value. But you never initialized in2.iArray[1] . in2.iArray[1] contains garbage. It is not surprising that in1.iArray[1] + in2.iArray[1] evaluates to garbage value that you see in the result.

The same is true for in2.iArray[2] and in2.iArray[3] . They too contain garbage.

The only meaningful additions your code makes are in1.iArray[4] + in1.iArray[4] and in1.iArray[5] + in1.iArray[5] . After that it adds garbage again, this time on in1 side: in1.iArray[6] , in1.iArray[7] and in1.iArray[8] contain garbage.

There is nothing strange, you can easily infer the result if you check the below aligned version.

            a[1] = 10     a[2] = 20    a[3] = 30   **a[4] = 40 a[5] = 50** 
                                                   **b[4] = 40 b[5] = 50**  b[6] = 60 b[7] = 70 b[8] = 80
 c[0] = 0   c[1] = 32777  c[2] = 100   c[3] = 38   **c[4] = 80 c[5] = 100** c[6] = 60 c[7] = 74 c[8] = 80 c[9] = 32767

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM