473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Struct/Class reassignments?

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

Apr 4 '07 #1
3 1641
On Apr 3, 11:17 pm, jetwe...@hotmail.com wrote:
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.

Apr 4 '07 #2
On Apr 4, 3:17 pm, jetwe...@hotmail.com wrote:
>
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.

Apr 4 '07 #3
On Apr 4, 12:02 am, "Old Wolf" <oldw...@inspire.net.nzwrote:
On Apr 4, 3:17 pm, jetwe...@hotmail.com wrote:
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.

Apr 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
5
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
1
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.