J
jetweedy
Hi,
I'm trying to figure out how I can reassign values within an array of
structures. I'm trying to write a really simple text game to learn
some C++, and I can't for the life of me find information on how to
make a simple change to a value found in a structure stored in an
array. I'm storing map squares in a 2D array of structures (have also
tried with classes - I'm still unclear on the difference, and welcome
recommendations) . The first dimension is X (from left to right), and
the second is Y (from top to bottom), with 0,0 (therefore [0][0])
being the top left. Here's what I started with:
struct space
{
bool isClear;
};
space mapspace[3][3];
mapspace[0][0]->isClear = true; // <--- Error Line
I'm getting the following error (line marked above):
g.cpp:20: error: expected constructor, destructor, or type conversion
before '.' token
I've also tried this, and I get the same error:
Class Space
{
bool isClear;
};
Space mapspace[3][3];
mapspace[0][0].isClear = true; // <--- Error Line
I even tried just using a boolean array:
bool isSpaceClear[3][3];
isSpaceClear[0][0] = true; // <--- Error Line
Since I'm getting the same error no matter what kind of array I make,
I'm obviously missing some point about referencing array items in C++
in order to assign a new value. But if I reference them in this way to
retrieve a value (for example, if I just directly assign the entire
structure using lots of curly braces, which works fine), then the
name[#][#] format works just fine.
bool isClear = mapspace[0][0].isClear; //<-- gives me the correct
value
mapspace[0][0].isClear = true; // <-- returns the error described
The above results are observed whether I use an array of structures or
Classes.
What am I missing? I've been scouring Google Groups for hours now, and
I can't find anything in the primer or the 'easy steps' books that
I've been using to learn.
Thanks in advance,
Jon
I'm trying to figure out how I can reassign values within an array of
structures. I'm trying to write a really simple text game to learn
some C++, and I can't for the life of me find information on how to
make a simple change to a value found in a structure stored in an
array. I'm storing map squares in a 2D array of structures (have also
tried with classes - I'm still unclear on the difference, and welcome
recommendations) . The first dimension is X (from left to right), and
the second is Y (from top to bottom), with 0,0 (therefore [0][0])
being the top left. Here's what I started with:
struct space
{
bool isClear;
};
space mapspace[3][3];
mapspace[0][0]->isClear = true; // <--- Error Line
I'm getting the following error (line marked above):
g.cpp:20: error: expected constructor, destructor, or type conversion
before '.' token
I've also tried this, and I get the same error:
Class Space
{
bool isClear;
};
Space mapspace[3][3];
mapspace[0][0].isClear = true; // <--- Error Line
I even tried just using a boolean array:
bool isSpaceClear[3][3];
isSpaceClear[0][0] = true; // <--- Error Line
Since I'm getting the same error no matter what kind of array I make,
I'm obviously missing some point about referencing array items in C++
in order to assign a new value. But if I reference them in this way to
retrieve a value (for example, if I just directly assign the entire
structure using lots of curly braces, which works fine), then the
name[#][#] format works just fine.
bool isClear = mapspace[0][0].isClear; //<-- gives me the correct
value
mapspace[0][0].isClear = true; // <-- returns the error described
The above results are observed whether I use an array of structures or
Classes.
What am I missing? I've been scouring Google Groups for hours now, and
I can't find anything in the primer or the 'easy steps' books that
I've been using to learn.
Thanks in advance,
Jon