• 12
• 12
• 9
• 10
• 13

# data structures

This topic is 4171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi. im using data structures which for some reason arent working. using the following structure works:
typedef struct
{
f32 x, y, z;
} waypoint;

s32 waypointLimit = 4;
waypoint Waypoint1[4];

Waypoint1[3].y = 8192;
however it doesnt work when i try:
typedef struct
{
f32 shipspeed = 0;
s32 polycount = 0;
s32 oldpolycount = 0;
s32 frameCount = 0;
int tilt = 0;
f32 gravity = 0;
f32 transmatrix[16];
s32 currentWaypoint = 0;
f32 tempangle;
f32 t;
f32 cosangle;
f32 sinangle;
} plr;

plr player[6];

player[5].gravity += 1600;

im getting a "structure <anonymous> has no member named transmatrix" (or whatever member i use). what am i doing wrong? i'm aware on the different methods of using data structures but if the first example works, why shouldnt the second? [Edited by - Rajveer on October 19, 2006 12:45:35 PM]

##### Share on other sites
Your index 6 is out of bounds, although that is not the problem.

What language are you using? The "f32 shipspeed = 0;" is completely alien to me in both C89 and C99 (and C++, but that's obviously not the language here).

EDIT: wait a minute... I think I can remember that non-ISO C++ allows such initialization, although I do not remember what the fields were initialized to. If you are using a non-ISO compiler (such as Visual C++ 6), your fields may be transformed into static variables, which prevents them from being accessed as members. Either way, if that is the case, your problem stems from language misuse.

##### Share on other sites
cheers for the out of bounds error, was an error when typing in the post, thanks!

im using c++, how do you mean f32 shipspeed = 0; is alien to you in c?

im using the newlib c++ library, its incorporated in devkitpro which im using for nintendo ds development. it has its own compiler, not sure quiet what you mean by non-iso (still a beginner!)

one thing im confused about is that the first example works, whereas the second doesnt, although theres nothing too different between them!

##### Share on other sites
How do you it mean doesn't work? Does the compiler give an error message?

f32 shipspeed = 0;
I've never seen variables initialised in a struct like that and I don't think it is standard - this is what ToohrVyk was referring to I believe.

Try not initialising the variables in the struct and see if it works~
f32 shipspeed;
Is standard.

Another small point -
typedef struct{	f32 x, y, z;} waypoint;

I'm wondering why you did typedef and then put waypoint after the struct, it compiles but that doesn't seem very c++ to me;
struct waypoint{	f32 x, y, z;};

##### Share on other sites
You have two problems here.
1. First, the "=0" part of the definition of a field does not exist: it is invalid syntax in C89, C99 and C++98. That your compiler allows it without even a warning is a very dire omen indeed, because it basically means that your compiler is not a C++ compiler. It might advertise itself as such, but it ultimately isn't. And, of course, since it's not a C++ compiler, there is no way for us to guess why id does something or doesn't without reading the manual that describes the actual language of the compiler (which, again, is not C++).

However, it is very possible that the "=0" part does what it did on older, pre-standard C++ compilers. This basically removes the variable from the class: it does not exist anymore as a member. Then, the variable is made a global variable, which can be accessed as ClassName::variableName. Note that since it is global, there is a single instance of it shared by all instances of the class.

2. Second, your code is actually C code. It uses C idioms and could be used without a hitch (aside from the "=0" part) on any ANSI C89 or C99 compiler. This is both good and bad news. The bad news is that you believe to be programming in C++, which you aren't (and, by extension, that you do not know what C++ is). C++ does not use the typedef struct {} name; idiom, for instance. There are possibly many other places in your code where you use similar C-isms. This, on the long term, will create problems once you start using actual C++ constructs, since these usually interact badly (that is, very verbosely and counter-intuitively) with straight-and-true C code. The good news, however, is that your C code appears to be pretty clean. If you stick to writing C as you are currently doing, then your project should be alright.

The C way of solving this is calling a function such as memset to initialize the array, or perhaps using a "zero" structure to overwrite every single element of your array.

The C++ way of solving this is creating a default constructor with an initializer list. However, obviously, this is not C++, so this approach would require severe code rewriting to be applied.

ISO is a standardization board. ISO C++ is what everyone refers to as "C++". Non-ISO C++ is an entire family of languages which advertise themselves as C++ but do not follow the "rules" of C++ that everyone is used to. For instance, Visual C++ uses a prestandard non-ISO C++ (it cannot compile certain correct C++ programs, because it does not use the same definition of a for loop), embedded devices use a lightweight version of C++ without exceptions, and certain Symbian OS versions further strip down C++ to remove its standard library and its global variables, resulting in a language that is as different from C++ as a snake is different from a plane.

ANSI is the equivalent, but for C. C89 is the 1989 standard of C, while C99 is the 1999 standard. The two languages are different (C99 is not entirely compatible with C89 and adds new features). C++0x refers to a future standard of C++, which should appear in 200x (x being unknown). It will include several additional libraries as part of its standard library.

C++ is a superset neither of C99 nor of C89, although it does share some features of C. The essential differences between C++ and C (besides the addition of several features to C++) is that the C++ library is used instead of the C library, and that several idioms have been abandoned or changed (pass-by-pointer, typedef struct, char*, etc).

##### Share on other sites
wow, ALOT of information which I am VERY grateful for! thanks for the info, every little helps a beginner!

for some reason, i didnt even realise i was equating the variables to 0 in the struct, which was the fault...:)

p.s. im not sure whether or not the compiler "complained" about the error, as i was given so many errors because of the "= 0" fault that i couldnt scroll to the top of command prompt and read the messages at the top

##### Share on other sites
Quote:
 p.s. im not sure whether or not the compiler "complained" about the error, as i was given so many errors because of the "= 0" fault that i couldnt scroll to the top of command prompt and read the messages at the top

You need to solve that problem first. Always start at the top of the error list, because many errors are caused by preceding ones. For instance, your initial error could have been caused by the compiler not recognizing f32 as a type. Compilation continues, but members with that type got ignored, so later on you get identifier not found errors. If you start at the bottom, you'll scratch your head going "but the identifier is right there?" If you start at the top, you'll fix the f32 issue, and never even see the identifier not found error farther down.

Aside:
Quote:
 Original post by ToohrVykFor instance, Visual C++ uses a prestandard non-ISO C++ (it cannot compile certain correct C++ programs, because it does not use the same definition of a for loop)...

I don't recall what the default behavior in 2003 was, but in 2005 the compiler defaults to standard for-scoping.

CM