In C++, all your variables need to be declared by name in your code. You are declaring a bunch of pointer variables all named line
in your loop, and then trying to use other named variables like health
, fatigue
, etc that have not been created.
I don't think you can directly create variables by name from a file like this, but you could read the file and create an array or vector of objects that contain the data from the file. You could pass the string read by getline()
into your Attributes
constructor, and then store the created pointers in an array or map that you can access later to call methods like display()
. If you really want a variable called health
in your code, it has to be declared somewhere in the code.
Another minor point is that you are reusing the variable name line
(which you previously declared to be a std::string) in the loop scope. This may work, but is confusing and should be avoided. Call your pointer variable something else, like attItem
.
For example:
Attributes * attItem = new Attributes(line);
attList.push_back(attItem);