I am trying to create a 2 dimensional array of chars as an attribute for my class, but the dimensions of this array are determined by a .txt file the script is reading in the constructor of the class, and I can't manage to initialize an attribute in the constructor.
I used the template for a class from the Qt creator and edited it as seen in this tutorial (in the "Creating custom signals and slots" chapter) : https://wiki.qt.io/Qt_for_Beginners and the method for reading the .txt file is from here : https://www.tutorialspoint.com/read-data-from-a-text-file-using-cplusplus .
Here are the header file
#ifndef GRID_H
#define GRID_H
#include <fstream>
#include "square.h"
using namespace std;
class Grid
{
private:
string text;
string line;
string directory;
int getSize();
const int height, width = getSize();
char * getGrid();
char grid [height][width]= getGrid();
public:
Grid(string directory);
};
#endif // GRID_H
and the source file of the class :
#include "grid.h"
Grid::Grid(string directory)
{
fstream newfile;
newfile.open(directory,ios::in); //open a file to perform read operation using file object
while(getline(newfile, line)){
text.append(line);
}
}
int Grid::getSize()
{
int h;
for (int ii = 1; ii > int(text.size()); ii++)
{
string part = "";
part += text[ii-1]+text[ii];
if (part.compare("\n"))
{
h++;
}
}
h++;
int w = line.size() - 2;
return h, w;
}
char * Grid::getGrid()
{
static char grid [height][width];
for (int y = 0; y > height; y++)
{
for (int x = 0; x > width; x++)
{
grid[y][x] = text[(width+2)*y + x];
}
}
return grid;
}
I know that this is full of errors but they were created when I tried to fix the initial error of having a "variable length array" in the first place (at line 35 of the source).
Here's the grid.txt for reference by the way :
..........
.......B..
..........
..........
..........
..........
..........
..........
.A........
..........