Home > Mobile >  How to overload operator in a Template array class to add every element with the same Index togeth
How to overload operator in a Template array class to add every element with the same Index togeth

Time:05-18

I somewhat successfully overloaded the operator to add 2 arrays of the same size together.

This is my current code

//.h 
#pragma once
#include <iostream>

template<typename T, size_t S>
class MyArray {
    public:
        T dataArray[S];

        T& operator[](size_t arrayIndex);
        const T& operator[](size_t arrayIndex) const;

        MyArray<T, S> operator (const MyArray& secondSummand) const;

        constexpr size_t getSize() const;

        const T at(unsigned int arrayIndex) const;

        void place(int arrayIndex, T arrayValue);

        T* acces();//pointer to first array element
};

template<typename T, size_t S>
inline constexpr size_t MyArray<T, S>::getSize() const {
    return S;
}

template<typename T, size_t S>
inline T& MyArray<T, S>::operator[](size_t arrayIndex) {
    return dataArray[arrayIndex];
}

template<typename T, size_t S>
inline const T& MyArray<T, S>::operator[](size_t arrayIndex) const {
    return dataArray[arrayIndex];
}

template<typename T, size_t S>
inline MyArray<T, S> MyArray<T, S>::operator (const MyArray& secondSummand) const {

    MyArray returnArray{};
    
    for (unsigned int i = 0; i < S; i  ) {
        returnArray[i] = this->at(i)   secondSummand[i];
    }
    return returnArray;
}

template<typename T, size_t S>
inline const T MyArray<T, S>::at(unsigned int arrayIndex) const {
    return dataArray[arrayIndex];
}

template<typename T, size_t S>
inline void MyArray<T, S>::place(int arrayIndex, T arrayValue) {
    dataArray[arrayIndex] = arrayValue;
}

template<typename T, size_t S>
inline T* MyArray<T, S>::acces() {
    return dataArray;
}
//main.cpp

#include <iostream>
#include <random>

#include "MyArray.h"

int main() {
    {
        srand((unsigned)time(0));
    
        //Working fine
        MyArray<int, 5> firstArray = {10, 5, 3, 2, 8};
        MyArray<int, 5> secondArray = {5, 3, 5, 6, 2};
    
        std::cout << "The first Array numbers are:\n";
        for (unsigned int i = 0; i < firstArray.getSize(); i  ) {
            std::cout << firstArray[i] << " ";
    
        }
    
        std::cout << "\n\nThe second Array numbers are:\n";
    
        for (unsigned int i = 0; i < secondArray.getSize(); i  ) {
            std::cout << secondArray[i] << " ";
        }
    
        MyArray<int, firstArray.getSize()> tempArray = firstArray   secondArray;
    
        std::cout << "\n\nAdd every position of 2 Arrays together:\n";
    
        for (unsigned int i = 0; i < tempArray.getSize(); i  ) {
            std::cout << firstArray[i] << "   " << secondArray[i] << " = " << tempArray[i] << "\n";
        }
    
    }

    //Not working
    MyArray<int, 5> firstArray = {10, 5, 3, 2, 8};
    MyArray<int, 4> secondArray = {5, 3, 5, 6};

    std::cout << "\n\nThe first Array numbers are:\n";
    for (unsigned int i = 0; i < firstArray.getSize(); i  ) {
        std::cout << firstArray[i] << " ";

    }

    std::cout << "\n\nThe second Array numbers are:\n";

    for (unsigned int i = 0; i < secondArray.getSize(); i  ) {
        std::cout << secondArray[i] << " ";
    }
}

So my overloaded operator works fine for objects (array) with the same size. If i try to add 2 objects with different sizes i get this error that the type is not the same

https://i.stack.imgur.com/7cZG4.png

if my understanding is correct my return type of the operator is an MyArray object that has the same Template arguments as the summand on the left side of .

in my second example "Not working" this should be T = int, S = 5 and the left side of the operator would be a const Reference to my array with T = int, S = 4

i dont understand why this is not working because i did the same without templates and it worked fine, can someone explain to me why i cant add 2 arrays with different sizes together with my code or what i can do so that it accepts objects with different sizes?

thank you in advance

CodePudding user response:

In the declaration of the operator function:

MyArray<T, S> operator (const MyArray& secondSummand) const;

When you use plain MyArray it's implied to be MyArray<T, S>. The template arguments T and S are the same as for both "this" class and the class of the function argument.

If you want to use different sizes for your argument then you need to make the operator function a template as well, to provide for the different size of the

template<size_t R>
MyArray<T, S> operator (const MyArray<T, R>& secondSummand) const;

Note that I kept the size of the resulting array object the same size as "this" array object, as per the OP's comment.

Be careful to not go out of bounds when implementing the operator, you should only loop to the minimum of the two objects sizes, and if S > R then the rest of the result array should probably be zero-initialized.

  • Related