-1

The task is to create 2 variables of the Array class to populate them, output, add and multiply. I created 2 variables of the Array class, after which I wanted to add them, I wrote an addition operator for this, but it does not return an array of sums of the other two arrays

Code:

#include <iostream>
#include <fstream>
#include <string>

void showMenu() {
    std::cout << "-------Menu-------" << std::endl <<
        "1-Input matrix" << std::endl <<
        "2-Print matrix" << std::endl <<
        "3-Sum matrix" << std::endl <<
        "4-Multiply matrix" << std::endl <<
        "0-Exit" << std::endl <<
        "------------------" << std::endl;
}

class Array {
public:
    Array(const int size) {
        this->size = size;
        arr = new int[this->size];
    }
    void fillArr() {
        std::cout << "Enter elements of array: ";
        for (size_t i = 0; i < size; i++) {
            std::cin >> arr[i];
        }
    }

    int getSize() {
        return size;
    }

    int& operator [] (const int index) {
        return arr[index];
    }
    
    void showArr() {
        for (size_t i = 0; i < size; i++) {
            std::cout << arr[i] << '\t';
        }
        std::cout << std::endl;
    }
    ~Array() {
        delete[] arr;
    }
private:
    int size = 0;
    int* arr;
};

Array operator + (Array arr1, Array arr2) {
    int temp = 0;
    if (arr1.getSize() < arr2.getSize())
        temp = arr1.getSize();
    else temp = arr2.getSize();
    Array tempArr(temp);

    for (size_t i = 0; i < temp; ++i) {
        tempArr[i] = arr1[i] + arr2[i];
        
    }
    tempArr.showArr();
    return tempArr;
}

Array operator * (Array arr1, Array arr2) {
    int temp = 0;
    if (arr1.getSize() < arr2.getSize())
        temp = arr1.getSize();
    else temp = arr2.getSize();
    Array tempArr(temp);

    for (size_t i = 0; i < temp; ++i) {
        tempArr[i] = arr1[i] * arr2[i];
    }

    return tempArr;
}



std::int16_t main() {
    int num = 0;
    int size1 = 0, size2 = 0;

    std::cout << "Enter size of first array: ";
    std::cin >> size1;
    std::cout << "Enter size of second array: ";
    std::cin >> size2;
    Array arr1(size1), arr2(size2);
    while (true) {
        showMenu();
        std::cout << "Choice: ";
        std::cin >> num;

        switch (num) {
        case 1:
            arr1.fillArr();
            arr2.fillArr();
            break;
        case 2:
            arr1.showArr();
            arr2.showArr();
            break;
        case 3: {
            Array temp(arr1 + arr2);
            temp.showArr();
            break;
        }
        case 4:
            (arr1 * arr2).showArr();
            break;
        }
    }
}

I tried to change the array and the operator itself, but nothing came out. Help understand the problem

Blinovich
  • 19
  • 2
  • Why don't you just use `std::array` and/or `std::vector`? – Jesper Juhl Nov 13 '22 at 00:23
  • it's too easy, I want to figure out how it works – Blinovich Nov 13 '22 at 00:27
  • 1
    Then look at the implementation of `std::array` and `std::vector` in one of the several open source standard libraries. – Jesper Juhl Nov 13 '22 at 00:31
  • 1
    `main` returns `int`. – Pete Becker Nov 13 '22 at 00:32
  • 1
    what did you see when you stepped through with your debugger – pm100 Nov 13 '22 at 00:34
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Evg Nov 13 '22 at 00:34
  • I use debugger. In the statement, everything works, even the array is displayed, but in the main everything breaks and garbage appears in the array – Blinovich Nov 13 '22 at 00:37
  • Replace every instance of `<< std::endl <<` with `"\n"`, it's less cluttered. By the way, how do the arrays get copied when passed as arguments to `operator+`? – QuentinUK Nov 13 '22 at 00:38
  • 1
    "it does not return an array of sums of the other two arrays." So what *does* it return? (See also: Rule of three, which may be the source of the problem.) – Raymond Chen Nov 13 '22 at 00:41
  • There is almost never a reason to have a `showMenu` function in a [mre]. Don't give us a choice about what steps to take. I would expect the main function of *example* code to look more like `int main() { Array arr1(1); Array arr2(1); arr1[0] = 1; arr2[0] = 2; Array sum(arr1 + arr2); sum.showArr(); }`. – JaMiT Nov 13 '22 at 00:50

1 Answers1

0

you need to read up on the rule of 3

here

Array tempArr(temp);

for (size_t i = 0; i < temp; ++i) {
    tempArr[i] = arr1[i] + arr2[i];

}
tempArr.showArr();
return tempArr;

you return a copy of an Array instance. In that case both instances are pointing at the same newed array (arr). The first one to go out of scope (when you exit the operator + function) will delete that data, hence giving you garbage. Then when the second one is destroyed (at the break statement in the menu switch) it tries to delete the same array again., on my machine that errors out

https://en.cppreference.com/w/cpp/language/rule_of_three

pm100
  • 48,078
  • 23
  • 82
  • 145