Onur
Onur

Reputation: 3

How can I overload an operator on created pointer object from template class?

I created a template class which named with Matrix. I'm trying to figure out how to implement an operator+ overload for a pointer which is pointed an Matrix object. My object is scalar matrix operations. I want to add an integer value to each cells on the matrix. I could not understand what is going on my code. The templates have confused me a lot.

"m1" is my Matrix object. When I use m1 = m1+2, I'm getting segmentation fault error.

I have tried many different combinations, but I get the same error all the time.

Matrix <int> * m = new Matrix <int> (10,10,2)
return m;

My Matrix class:

template <typename T> 
class Matrix{
    vector< vector<T> > matris;
public: 
    Matrix(); // 10x10 matrix with zeros
    Matrix(int width, int height, int value); 
    void print();

    Matrix* operator+(T value){
        return (new Matrix<int>(10,10,2)); // 10x10 matrix filled with 2
    }
};

My main function:

int main(){
    Matrix <int> *m1 = new Matrix <int>(); // 10x10 matrix with zeros
    m1->print(); // it succesfully shows the elements
    m1 = m1 + 2; // whenever I tried this, I get "Segmentation Fault"
    m1->print();

    return 0;
}

My print() function:

template <typename T> 
void Matrix<T>::print(){
    for(int h=0; h<matris.size(); h++){
        for(int w=0; w<matris[0].size(); w++){
            printf("%4d", matris[h][w]);
        }
        cout << endl;
    }
}

Output:

   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0
Segmentation fault

My expectation is that the assignment was successful, but I get the segmentation fault error. What am I doing wrong here?

Upvotes: 0

Views: 60

Answers (1)

R Sahu
R Sahu

Reputation: 206627

When you use

 m1 = m1 + 2;

the overloaded operator+ is not used. That is just pointer arithmetic. After that, m1 points to memory that you don't own. Accessing members of m1 after that causes undefined behavior.

You can fix that syntactically by using

 m1 = *m1 + 2;

but it has its own problems. When you do that, the original memory m1 was pointing to is lost to your program. You have a memory leak.

Providing the overload as

Matrix* operator+(T value) { ... }

is not idiomatic. Perhaps you want to use:

Matrix operator+(T const& value) const { ... }

A better alternative would be to use couple of non-member function overloads:

Matrix operator+(Matrix const& m, T const& value);
Matrix operator+(T const& value, Matrix const& m);

With that, you can use:

Matrix<int> m1;
Matrix<int> m2 = m1 + 10;
Matrix<int> m3 = 30 + m1;

More on operator overloads can be found at What are the basic rules and idioms for operator overloading?

Upvotes: 2

Related Questions