Problem when printing c ++ array

0

I'm trying to read an array and print it completely, but only the last line is coming out and I do not 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's just the source file "array.cpp". The class definition and the main function are not there, if necessary I link to them as well.

edit: As requested the "array.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
    
asked by anonymous 06.09.2017 / 04:31

1 answer

1

As commented out, 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 it is not initializing the fields but rather local variables in the constructor.

Simply put, this would be the right thing to do.

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

In addition, you have created a 0x0 array and you can not resize it. To make an array of specifiable size, you have two options: template or pointer. By defining the class with templates, you can create arrays with predefined sizes (for example,% wma% is a matrix of two rows and three columns, you can not change). With pointers, whenever you determine the size of the array you allocate cells that support that size.

This solves a lot of the problems, I do not know if it solves all of them so that the program works. If you have more problems or have any questions, please let us know.

    
06.09.2017 / 06:06