Struct/Class reassignments?

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
 
D

dragoncoder

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
Right, because mapspace[0][0] is not a pointer but a struct space.
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
This is correct syntax. So either your compiler is broaken or the
compiler is not seeing above written code. Did you save it?
BTW which compiler are you using?
I even tried just using a boolean array:

bool isSpaceClear[3][3];
isSpaceClear[0][0] = true; // <--- Error Line
That makes me even more confident that this is certainly not the code
the compiler is compiling.

You are doing right except the first way.
 
O

Old Wolf

struct space
{
bool isClear;
};

space mapspace[3][3];
mapspace[0][0]->isClear = true; // <--- Error Line

Should be:
mapspace[0][0].isClear = true;
I'm getting the following error (line marked above):
g.cpp:20: error: expected constructor, destructor, or type conversion
before '.' token

Well, that error doesn't seem to correspond to the code you wrote.
I've also tried this, and I get the same error:

Class Space

You should have got a compile error on that line, unless you
have pre-defined 'Class' to mean something..
{
bool isClear;
};

Space mapspace[3][3];
mapspace[0][0].isClear = true; // <--- Error Line

You would get an error because isClear is private and you cannot
modify private data from outside of the class's member functions.
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++

Well, it's hard to say for sure because obviously you have not
posted your exact code.

I'll take a wild stab in the dark; perhaps you are trying the above
code outside of a function? Outside of a function you are only allowed
to have declarations; you cannot have assignment statements.

If you post the exact, entire contents of "g.cpp" then we will be
able to give you a more definite answer.
 
J

jetweedy

struct space
{
bool isClear;
};
space mapspace[3][3];
mapspace[0][0]->isClear = true; // <--- Error Line

Should be:
mapspace[0][0].isClear = true;
I'm getting the following error (line marked above):
g.cpp:20: error: expected constructor, destructor, or type conversion
before '.' token

Well, that error doesn't seem to correspond to the code you wrote.
I've also tried this, and I get the same error:
Class Space

You should have got a compile error on that line, unless you
have pre-defined 'Class' to mean something..
{
bool isClear;
};
Space mapspace[3][3];
mapspace[0][0].isClear = true; // <--- Error Line

You would get an error because isClear is private and you cannot
modify private data from outside of the class's member functions.
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++

Well, it's hard to say for sure because obviously you have not
posted your exact code.

I'll take a wild stab in the dark; perhaps you are trying the above
code outside of a function? Outside of a function you are only allowed
to have declarations; you cannot have assignment statements.

If you post the exact, entire contents of "g.cpp" then we will be
able to give you a more definite answer.


That last bit makes sense now. It's outside of any function. I'll
place it inside of a setup function, call it in main(), and try it
again. I'm not somewhere that I can test it out at the moment, but now
that you mention that, I'm sure it's the reason. Thanks to both of you
for the helpful replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top