0

I have a matrix2d class which consists of a dobule A[2][2]. I am trying to do a constructor which takes obejct of the same type and copies all its values to A[2][2]. I have a problem, here is the class:

class matrix2D {
    public:
    double A[2][2];
    double Z[2][2];

    //Default Constructor.
    matrix2D() {
        A[0][0] = A[1][1] = 1;
        A[0][1] = A[1][0] = 0;
    }

    matrix2D(double x00,double x01, double x10, double x11) {
        A[0][0] = x00;
        A[0][1] = x01;
        A[1][0] = x10;
        A[1][1] = x11;
    }

and now I am to create a constructor which takes matrix2D object and then takes all its values to A.

// Copy Constructor.
    matrix2D(matrix2D& Z) {
        for(int i = 0; i < 2; ++i) {
            for(int j = 0; j < 2; ++j) {
                   A[i][j]=*(Z[i][j]);
            }
        }
    }

It tells my that I try to assign double to matrix2d object. Why does *Z[i][j] does not reference to a double?

SOLVED: I did A[i][j]=Z.A[i][j] :)!

5
  • Same error: no match for 'operator[]' (operand types are 'matrix2d' and 'int') Commented Sep 30, 2016 at 15:21
  • 2
    Why the *? There is no pointer involved. Commented Sep 30, 2016 at 15:23
  • Just let the compiler do its job. It will make a copy constructor that does the right thing. Commented Sep 30, 2016 at 15:24
  • You have Z as both a member variable of your class and as a constructor parameter. The compiler might give you a warning about that. Commented Sep 30, 2016 at 15:26
  • You are confusing reference and pointer. Your argument is a reference on a 2 dimension table. Z[i][j] already gives you the value of the matrix at (i, j). References (&) are supposed to be transparent pointers (*) ! Commented Sep 30, 2016 at 15:27

4 Answers 4

1

The * in that line does not make sense.

Given the data, you don't need a copy constructor at all. However, if you must implement one, it needs to be something along the lines of:

// Use const&, not just &.
// Use a more suitable variable name for the copy
matrix2D(matrix2D const& copy) {
    for(int i = 0; i < 2; ++i) {
        for(int j = 0; j < 2; ++j) {
               A[i][j]= copy.A[i][j];  // Copy A
               Z[i][j]= copy.Z[i][j];  // Copy Z.
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No, what is needed is to remove the copy constructor entirely!
1

There are problem in your copy constructor, you are shadowing one of the members (member name Z and parameter name Z).

I would suggest to not write your own copy constructor and let compiler generate one for you:

matrix2D(const matrix2D & value) = default;

Comments

1

Adding a second answer. And this is the answer I prefer.

Just remove the copy constructor altogether from your class declaration and definition.

Given that your class contains just a pair of fixed sized arrays, you don't need a copy constructor. The compiler will auto generate one for you. A custom copy constructor is typically only needed when your class has dynamically allocated member variables and you need to insure that the pointer values aren't aliased across instances.

Comments

0

You can certainly do the for-loop technique - and I think that would be good to help in your understanding of arrays and pointers. But it's hard to beat the efficiency of memcpy to copy an array of simple types.

   // Copy Constructor.
    matrix2D(const matrix2D& other) {

        memcpy(A, other.A, sizeof(A));
        memcpy(Z, other.Z, sizeof(Z));

    }

1 Comment

how to do it without memcpy?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.