Problem printing matrix c++

Asked

Viewed 441 times

0

I’m trying to read an array and print it completely, but it’s only coming out the last line and I don’t know why.

#include "matriz.hpp"
#include <iostream>

Matriz::Matriz(){
  char matriz[0][0];
  int quantidade_linhas=0 ;
  int quantidade_colunas=0 ;
}

int Matriz::getLinhas(){
  return quantidade_linhas;
}

void Matriz::insere_Linhas(){
  cout << "Digite a quantidade de linhas desejadas: ";
  cin >> quantidade_linhas;
  this -> quantidade_linhas= quantidade_linhas;
}

int Matriz::getColunas(){
  return quantidade_colunas;
}

void Matriz::insere_Colunas(){
  cout << "Digite a quantidade de colunas desejadas: ";
  cin >> quantidade_colunas;
  this -> quantidade_colunas = quantidade_colunas;
}

void Matriz::cria_Matriz(){
  matriz [quantidade_linhas][quantidade_colunas];
  }

void Matriz::imprime_matriz(){
  int linhas = getLinhas();
  int colunas = getColunas();

  for(int i=0 ; i<linhas ; i++){
    for(int j=0; j<colunas ; j++){
      cin >> matriz[i][j] ;
    }
  }

  for(int i=0 ; i<linhas ; i++){
    for(int j=0; j<colunas ; j++){
      cout << matriz[i][j] ;
    }
  }

}

It is only the source "matrix.cpp". The class definition and the function main are not there, if necessary I put the link with them too.

Edit: How was requested the "matrix.hpp" here is

#ifndef MATRIZ_HPP
#define MATRIZ_HPP

#include <iostream>
using namespace std;

class Matriz{

private:
char matriz[0][0];
int quantidade_linhas;
int quantidade_colunas;
int geracoes;
bool cel_viva;
bool cel_morta;


public:
Matriz();

void insere_Linhas();
int  getLinhas();
void insere_Colunas();
int getColunas();
void cria_Matriz();
void imprime_matriz();

};
#endif
  • Please put the "matrix.hpp" so that we know the fields that have been defined and their types. I say that the constructor seems to have problems, because it declares local variables and initializes them instead of assigning values to fields.

  • I’ve already updated the post with the.hpp matrix

1 answer

1


As commented, you created fields in the structure but did not assign values in the constructor. When you define something in the constructor, you define local variable (as in any function or method), so you are not initiating fields but local constructor variables.

Simply put, it would be right.

Matriz::Matriz(){
  //char matriz[0][0] ;
  quantidade_linhas = 0 ;
  quantidade_colunas = 0 ;
}

In addition, you have created an 0x0 matrix and you cannot modify the size. To make a matrix of specific size, you have two options: template or pointer. By setting the class with templates, you can create array types with predetermined sizes (for example, Matriz<3,2> is two-row and three-column matrix, cannot change). With pointers, whenever determining the size of the matrix you allocate cells that support this size.

That solves a lot of the problems, I don’t know if it solves them all in a way that the program works. If you have more problems or any questions, please inform.

Browser other questions tagged

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