error creating matrix with variables in c++

Asked

Viewed 282 times

0

When I declare matrices in c++ within a class, defining "rows' and "columns" of that matrix by integer numbers directly with arguments, example [2][5] I can have the correct functioning.

However when I use variables within the matrix arguments it gives me a compilation error. (thus below)

     private:
       int x;
       int y;
       float matriz[x][y];

Error: invalid use of non-static data Member Matrix::x invalid use of non-static data Member Matrix::y

(this program I’m trying to do, is basically a matrix to represent set of real numbers).

  • 1

    You must set a fixed value for the matrix, unfortunately there is no way to put a "variable" with no defined value between the matrix brackets.

  • Use the new operator for dynamic allocation.

  • right, how should I do that? , I haven’t had much experience with matrix.

1 answer

2


As noted in one of the comments, you can dynamically allocate memory to the parent member. Below an example (Obs. I put a function to fill and another to print the created matrix. I think this makes it easier to understand the example):

#include <iostream>

class MatrizDinamica{

private:
   int x;
   int y;   
   //declara a matriz como um ponteiro de ponteiros
   float** matriz;

public:
   MatrizDinamica(int rows, int columns){
      x = rows;
      y = columns;      
      //aloca a memória dinamicamente
      matriz = new float*[x];
      for(int i = 0; i < x; i++){
         matriz[i] = new float[y];
      }
   }
   ~MatrizDinamica(){      
      //e não se esqueça de deletar no destructor a memória previamente alocada
     for(int i = 0; i < x; i++){
         delete[] matriz[i];
      }
      delete[] matriz;
   }

   void preencherMatriz(){
     int counter = 0;
     for(int i = 0; i < x; i++){
      for(int j = 0; j < x; j++){
         matriz[i][j] = (float) counter;
         counter++;
      }
     }
   }

   void imprimirMatriz(){
   for(int i = 0; i < x; i++){
      for(int j = 0; j < x; j++){
         std::cout << matriz[i][j] << "\t";
      }
      std::cout << "\n";
     }
   }
};

int main(){

   int rows, columns;
   std::cin >> rows >> columns;
   MatrizDinamica mMatriz(rows, columns);
   mMatriz.preencherMatriz();
   mMatriz.imprimirMatriz();
   return 0;
}

  • my real reason for this question is that I need to solve a problem where the user determines the size of the matrix, but your code is beyond my level but already gave to have a very clear idea, thank you.

  • Could I just do it by declaring the matrix as a pointer? Why does it have to be a pointer pointer? //.

  • 1

    @Victor, Pointer pointer is the equivalent of the two-dimensional matrix. If you only used pointer it would have only one dimension. So, if one dimension is only enough for your project, you can use a single pointer. Another detail: you came to consider using Std:vector?

  • Boy I was wanting to implement in a matrix sum exercise that the user typed the amount of rows and columns, but I ended up doing so a look. https://notepad.pw/share/ocme0uxa

Browser other questions tagged

You are not signed in. Login or sign up in order to post.