0

I am having trouble overloading multiplication operator for the purpose of array multiplication. How would you find the length of the array? I tried a.arr.length() for my for-loop but no luck.

Just curious, how exactly does the code know when to apply the overloading when multiplying two objects?

class Box {
public:
    int arr[5] = {1,2,3};
    int sum;
    int getarr() {
        return sum;
    }

    // Overload * operator to add two Box objects.


};
int operator*(const Box& a, const Box& b) {
    Box box;
    Box d;
    int sum = 0;
    for(int x = 0; x<3;x++){
        sum  += d.arr[x]  * b.arr[x];
    }

    return sum;
}

int main(){
    int sum = 0;
    Box box2;                // Declare Box2 of type Box
    Box box3;
    sum = box2 * box3;
    std::cout<<sum;

}
anon anon
  • 151
  • 4
  • 14
  • With an array, you can't access the length unless you have a separate variable that holds that number. If you want that functionality, you should use `std::vector`s not `array`s – Hawkeye5450 Feb 28 '18 at 05:17
  • You are aware that `int arr[5]` contains 5 numbers, even though the initializer only provides 3 explicit arguments? The initializer-list is implicitly extended with two zeroes. If you had provided _no_ initializer list, none of the integers would have been initialized, but you can't have a partial initializer. It's all-or-nothing. – MSalters Feb 28 '18 at 10:41

0 Answers0