how to make a step by reference of two-dimensional array to the constructor?

1

My program has a problem in being able to make a step by reference of two-dimensional arrays to the constructor of the class

 ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\pc\Downloads\Matrices-Costructores.cpp|50|error: declaration of 'matriz1' as array of references|
C:\Users\pc\Downloads\Matrices-Costructores.cpp|50|error: expected ')' before ',' token|
C:\Users\pc\Downloads\Matrices-Costructores.cpp|50|error: prototype for 'Matriz::Matriz(...)' does not match any in class 'Matriz'|
C:\Users\pc\Downloads\Matrices-Costructores.cpp|6|error: candidates are: constexpr Matriz::Matriz(const Matriz&)|
C:\Users\pc\Downloads\Matrices-Costructores.cpp|8|error:                 Matriz::Matriz(int&, int&, int&, int&, int&, int&, int&)|
C:\Users\pc\Downloads\Matrices-Costructores.cpp|50|error: expected unqualified-id before 'int'|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

The source code:

#include<iostream>
#include<cstdlib>

using namespace std;


class Matriz {
public:
  Matriz (int &, int &, int &, int &,  int &,  int &,  int &);
  ~Matriz (){};
  int   A[10][10], B[10][10], C[10][10], op, f1, f2, f3, c1, c2, c3;
};

int main (){
  int seleccion, m1, m2, n1, n2, matriz1[10][10], matriz2[10][10];
  cout<<"Este Programa Realiza Operaciones Con Matrices"<<endl;
  cout<<"       MENU:"<<endl
     <<"1.- Suma"<<endl
    <<"2.- Multiplicacion"<<endl
   <<"Seleccion: ";     cin>>seleccion;

  cout<<"Dame el numero de columnas de la matriz 1: "; cin>>m1;
  cout<<"Dame el numero de filas de la matriz 1: "; cin>>n1;
  cout<<"Dame los datos: "<<endl;

  for(int i=0;i<m1;i++){
    for(int j=0;j<n1;j++){
      matriz1[i][j]=0;
      cout<<"["<<i+1<<"]"<<"["<<j+1<<"]"; cin>>matriz1[i][j];
    }}

  cout<<"Dame el numero de columnas de la matriz 2: "; cin>>m2;
  cout<<"Dame el numero de filas de la matriz 2: "; cin>>n2;
  cout<<"Dame los datos: "<<endl;

  for(int i=0;i<m2;i++){
    for(int j=0;j<n2;j++){
      matriz2[i][j]=0;
      cout<<"["<<i+1<<"]"<<"["<<j+1<<"]"; cin>>matriz1[i][j];
    }}

  Matriz ob (matriz1[10][10], matriz2[10][10], seleccion, m1, m2, n1, n2);
  cout<<"\n\nMatriz Resultante: \n";
  for(int i=0;i<m1;i++){
    for(int j=0;j<n1;j++){
      cout<<matriz1[i][j]<<"\t";
    }cout<<endl;

  }
}
Matriz::Matriz(int &matriz1 [10][10], int &matriz2[10][10], int &seleccion, int &m1,  int &m2,  int &n1,  int &n2)
{
  op=seleccion;
  c1=m1;        c2=m2;      f1=n1;      f2=n2;
  for(int i=0;i<c1;i++){
    for(int j=0;j<f1;j++){
      A[i][j] = matriz1[i][j];
    }}

  for(int i=0;i<c2;i++){
    for(int j=0;j<f3;j++){
      B[i][j] = matriz2[i][j];
    }}

  switch (op){

  case 1:
    if (c1==c2 && f1==f2){
      c3=c1; f3=f1;

      for (int i=0;i<c3;i++){
        for(int j=0;j<f3;j++){
          C[i][j]=0;
          C[i][j]=A[i][j]+B[i][j];

          c1 = c3 ; f1 = f3;
          for(int i=0;i<c3;i++){
            for(int j=0;j<f3;j++){
              C[i][j] = matriz1[i][j];
            }}

        }} }else {
      cout<<"\n\nRecuerda que para realizar una suma, las filas\ny las columnas deben ser iguales entre ambas\nmatrices.";
    }

  case 2:
    if(c1==f2){
      c3=c1; f3=f2;
      for (int i=0;i<c3;i++){
        for (int j=0;j<f3;j++){
          C[i][j]=0;
          for (int k=0;k<f1;k++){
            C[i][j]=C[i][j]+A[i][k]*B[k][j];
          }
        }
      }

      c1 = c3 ; f1 = f3;
      for(int i=0;i<c3;i++){
        for(int j=0;j<f3;j++){
          C[i][j] = matriz1[i][j];
        }}
    } else {cout<<"No se puede realizar la multiplicacion"<<endl;}
  }
}
    
asked by Fernando Tarde 20.05.2018 в 20:55
source

2 answers

3

This statement:

Matriz (int &, int &, int &, int &,  int &,  int &,  int &);

Not compatible with this implementation:

Matriz::Matriz(int &matriz1 [10][10], int &matriz2[10][10], int &seleccion, int &m1,  int &m2,  int &n1,  int &n2)

And the reason is the first two arguments ... they just are not the same as expected.

It is easy to see if we work with variables that the following will not compile:

int matriz[10];
int variable = matriz; // ERROR: tipos incompatibles

And the same goes for the references:

int matriz[10];
int& ref = matriz; // ERROR: tipos incompatibles

Since an array of fixed size (which is what you are using) is equivalent to a pointer there is no need to pass a reference. The constructor's statement should look more like this:

Matriz (int [][10], int [][10], int &, int &,  int &,  int &,  int &);

And the implementation to this:

Matriz::Matriz(int matriz1 [][10], int matriz2[][10], int &seleccion, int &m1,  int &m2,  int &n1,  int &n2)

Note that the first dimension I have omitted ... is the only optional when working with this type of arrays ... although if you do not like it you can always specify all the dimensions (although the first one will be ignored by the compiler):

Matriz::Matriz(int matriz1 [10][10], int matriz2[10][10], int &seleccion, int &m1,  int &m2,  int &n1,  int &n2)

In any case, if the rest of the arguments are not going to be modified by the constructor, it is totally meaningless that they are references:

Matriz (int [][10], int [][10], int, int,  int,  int,  int);

Matriz::Matriz(int matriz1 [][10], int matriz2[][10], int seleccion, int m1,  int m2,  int n1,  int n2)

On the other hand, your program has another error and that is that the call to the constructor is not correct:

Matriz ob (matriz1[10][10], matriz2[10][10], seleccion, m1, m2, n1, n2);

Since it should be like this:

Matriz ob (matriz1, matriz2, seleccion, m1, m2, n1, n2);

Bonus

If the destructor is not going to have a specific behavior, it is best not to declare it and let the compiler be responsible for implementing it:

class Matriz {
public:
  Matriz (int [][10], int [][10], int, int,  int,  int,  int);
  // ~Matriz (){};
  int   A[10][10], B[10][10], C[10][10], op, f1, f2, f3, c1, c2, c3;
};

On the other hand, you can see in the error messages that you are compiling with at least the C ++ 11 standard. If you are still interested in explicitly putting the destructor, you should know that from C ++ 11 there is a new reserved word default that forces the compiler to create the default implementation:

class Matriz {
public:
  Matriz (int [][10], int [][10], int, int,  int,  int,  int);
   ~Matriz () = default;
  int   A[10][10], B[10][10], C[10][10], op, f1, f2, f3, c1, c2, c3;
};
    
answered by 21.05.2018 в 12:24
2
prototype for 'Matriz::Matriz(...)' does not match any in class 'Matriz'

This error is telling you that to build Matriz none of the existing constructors match the arguments that you have provided. And it's normal because you declare the Matriz constructor to receive seven integer references:

Matriz (int &, int &, int &, int &,  int &,  int &,  int &);

And you define it so that it receives two bidimensional 1 formations of references and five references to integers:

Matriz(int &matriz1 [10][10], int &matriz2[10][10], int &seleccion, int &m1,  int &m2,  int &n1,  int &n2)
declaration of 'matriz1' as array of references

You are declaring references training not references to training. In C ++ it is mandatory that the references refer to something (check out this answer ) and therefore the language prohibits References formations, because they could lead to references that do not point to anything.

The way to declare a reference to training is as follows:

tipo (&nombre)[rango1][rango2][...];

The parentheses surrounding the name are what will distinguish a reference formation from a reference to formation, if you want a reference to a formation of 10 × 10 integers this is the way to declare it:

int (&matriz1)[10][10];
  • Also known as array , or in English: array .
  • answered by 22.05.2018 в 08:10