0

The assignment operator overloading is never called.

In the main file in 1., copy constructor is called and in 2., first to be call is operator + then operator * and then default assignment operator is called.

Any reason for that problem?

template<int r, int c, class F = int>
class Matrix {

public:

    Matrix(){
        int i, j;
        data = new F[(r*c)];
        for (i = 0; i < c; i++){
            for (j = 0; j < r; j++){
                data[j + (i*c)] = 0;
            }
        }
    }
    ...

    Matrix(const Matrix& a){
        int i=r, j=c;
        data = new F[(r*c)];
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                data[j+(i*c)] = a.data[j+(i*c)];
            }
        }
    }
    **Matrix operator=(const Matrix a){
        int i, j;
        if (data != NULL){
            delete data;
            data = NULL;
        }
        data = new F[(r*c)];
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                data[j+(i*c)] = a.data[j+(i*c)];
            }
        }
        return *this;
    }**
    friend Matrix operator+( Matrix<r, c, F>& a, int b){
        Matrix<r, c, F> temp;
        int i, j;
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                temp.data[j+(i*c)] = a.data[j+(i*c)] + b;
            }
        }
        return temp;
    }
    Matrix operator*(const int a){
        Matrix <r, c, F> temp(*this);
        int i, j;
        for(i = 0; i < r; i++){
            for(j = 0; j < c; j++){
                temp.data[j+(i*c)] *= a;
            }
        }
        return temp;
    }
   ...


    ~Matrix(){
        delete[] data;
        data = NULL;
    }

private:
    F *data;


};



int main( ) {
    ...

    Matrix<4, 4> identity(1);
    std::cout << identity << std::endl;

    1.**Matrix<4, 4> res = identity;**

    Matrix<4, 4> identity_2(1);
    std::cout << identity _2<< std::endl;

    2.**Matrix<4, 4> res_2 = (identity_2 + 2) * 3;**
...
roy
  • 3
  • 3
  • 1
    `I have a problem` is not a diagnostic we can go on. – bolov Jun 01 '15 at 17:19
  • Why do you pass argument to `operator=` as `const Matrix` instead of `const Matrix &`? – hynner Jun 01 '15 at 17:20
  • What is the problem? It doesn't compile? If so please provide the full compile error message along with the line at which it appears. It runs, but gives unexpected output? If so please specify the expected output and the actual output. It compiles but crashes at runtime? If so please do a minimal debugging and at least specify the line at which it crashes. – bolov Jun 01 '15 at 17:22
  • Also in your `operator=` there is `data == NULL;` where it should probably be `data = NULL` – hynner Jun 01 '15 at 17:22
  • Because I can't dynamically allocate memory which I cant delete later because I must not change main file. – roy Jun 01 '15 at 17:28
  • You cannot format within a code block if that is why there are a couple `**`. Also is there a reason you are taking the `Matrix` by value in the assignment operator? – Guvante Jun 01 '15 at 17:48
  • It's one version i also tried to take the matrix by reference and it didn't work – roy Jun 01 '15 at 17:55
  • @roy: You need to `return this` and have the return type be `Matrix&` I updated my answer. – Guvante Jun 01 '15 at 18:55
  • @Guvante the problem isn't solved the assignment operator never called the only way I succeeded to call it is if in 2. I change the rerun values of operator + and * to be Matrix& but then I have to dynamically allocate memory wich I can't delete later. – roy Jun 02 '15 at 07:28
  • @roy: Why do you have to dynamically allocate memory for the assignment operator? It should just return `this`. It sounds like you have a few interdependent issues, if so you should try and separate them a bit to better explain them. – Guvante Jun 02 '15 at 17:19

1 Answers1

1
friend Matrix operator+( Matrix<r, c, F>& a, int b){
    Matrix<r, c, F> temp;
    int i, j;
    for(i = 0; i < r; i++){
        for(j = 0; j < c; j++){
            temp.data[j+(i*c)] = a.data[j+(i*c)] + b;
        }
    }
    return temp;
}

This code is confusing the non-member and member syntax for operators.

friend Matrix operator+( Matrix<r, c, F>& a, int b);

Means "there is a non-member function that has access to my internals". But what you defined defines a member function, which should have syntax

Matrix operator+(int b) {

Since this is implied as it is a member function. See this answer for an example of discussing the nuances.

Similarly your assignment operator is incorrect.

Matrix& operator=(const Matrix& a) {

Is the most common form, although the format of the parameter can change you need to return a reference for the return value.

Community
  • 1
  • 1
Guvante
  • 17,681
  • 1
  • 28
  • 64
  • 1
    I must commend you. I have long since lost the will to parse questions of the `I have a problem` variety. And also nice catch. – bolov Jun 01 '15 at 17:28