I want to initialize an array based on how many lines there are in a text file, so that I can read the text data into the program, but I am having issues with the struct. I so far understand that I need to define the array bounds with a constant, which I've tried to do, but I get errors saying that I am not defining with an integral constant-expression, though I believe that is what I am doing.
I am using g++ to compile my code with the C++17 standard. Below I included only relevant code.
//tools.h
int findMaxID()
{
std::ifstream fin("\data\items.txt");
char ch;
int z = 1;
while(fin)
{
fin.get(ch);
if(ch == '\n')
z++;
}
return z;
}
const int MaxID = findMaxID();
// data.h
#include"tools.h"
struct ItemData
{
public:
unsigned int ID[::MaxID];
char Name[::MaxID][32];
char Type[::MaxID][8];
};
data.h:15:21: error: size of array 'ID' is not an integral constant-expression
15 | unsigned int ID[::MaxID];
| ~~^~~~~
data.h:16:15: error: size of array 'Name' is not an integral constant-expression
16 | char Name[::MaxID][32];
| ~~^~~~~
data.h:17:15: error: size of array 'Type' is not an integral constant-expression
17 | char Type[::MaxID][8];
I'm stumped as to what to do. I've tried absolutely everything, I tried using vector instead of arrays, but I found it awkward and confusing when using it multi-dimensional, and I tried using a pointer to the value. Maybe I set it up wrong. All I know is that I've tried my absolute best to overcome this, but the problem doesn't seem to be in my reach to solve. I hope you can help me with this issue.