Page 1 of 1
C++ structs
Posted: Tue Jan 03, 2006 6:34 am
by Sachiel7
Those daggone structs!
I'm having issues creating nested structs in DevC++ (4.9.9.2)
Here's an example:
Code: Select all
struct TestOne {
int X;
int Y;
};
struct TestTwo {
TestOne OBJ;
int Z;
};
TestTwo ITEM;
ITEM.OBJ.X = 1;
error occours at the last line. "expected constructor, destructor, or type conversion before '.' token."
I've never used nested structs in C++ before, and all the reference sites I look at list examples similar to this one, even for DevC++ specifically.
It has been a while since I've progged C++, I've been picking it back up now for a few months (mainly working w/ irrlicht)
What am I doing wrong?
Posted: Tue Jan 03, 2006 6:50 am
by TreyOne Games
Posted: Tue Jan 03, 2006 6:50 am
by TreyOne Games
Posted: Tue Jan 03, 2006 6:54 am
by jam
created this small program using your code and it compiles just fine.
Code: Select all
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
struct TestOne
{
int X;
int Y;
};
struct TestTwo
{
TestOne OBJ;
int Z;
};
TestTwo ITEM;
ITEM.OBJ.X = 1;
printf("x = %d\n",ITEM.OBJ.X);
system("pause");
return 0;
}
Posted: Tue Jan 03, 2006 7:08 am
by bitplane
hmm my money is on "ITEM.OBJ.X = 1;" being outside main()
Posted: Tue Jan 03, 2006 8:53 am
by Sachiel7
Yes it was placed outside main().
What's the difference?
I want a global variable of a nested structure...
Not sure why DevC++ don't like it
PS - sticking it in main() worked, but how can I make it global then?
Or should i just define it globally, and alter it within main() or other functions?
Posted: Tue Jan 03, 2006 9:21 am
by Sachiel7
Ok, hang on,
I tried my said idea above ^ (define globally, modify from within functions)
and all is working well so far...
Now just for the file loader...
Thanks for the help btw guys,
I'm working on a simple but effective camera scripting engine to plugin to my games. So far, so good. It runs if I manually code a script, but I'm going to add a loadFromFile function.
Thanks!
Posted: Tue Jan 03, 2006 9:25 am
by bitplane
maybe this helps:
Code: Select all
int i=0; // legal
int j;
j=0; // illegal
struct thing
{
int x=0; // illegal (i think)
};
struct thing2
{
int x;
} foo;
foo.x = 0; // also illegal
class thing3
{
public:
int x;
thing3() : x(0)
{
// legal
}
}
Posted: Thu Jan 12, 2006 7:49 am
by Guest
Provided that your struct is an aggregate, you s/could initialize your global instance using the aggregate initializer syntax. There are some advantages to doing this over adding a constructor as proposed above.
struct TestOne
{
int X;
int Y;
};
struct TestTwo
{
TestOne OBJ;
int Z;
};
TestTwo ITEM = { { 0, 0 }, 1 };
Posted: Thu Jan 12, 2006 8:00 am
by Guest
Code: Select all
struct TestOne {
int X;
int Y;
};
struct TestTwo {
struct TestOne OBJ;
int Z;
};
[b]struct[/b] TestTwo ITEM;
ITEM.OBJ.X = 1;
if you want to avoid the struct in the declaraction, just do this:
Code: Select all
[b]typedef[/b] struct TestOne {
int X;
int Y;
} [b]TestOne[/b];
[b]typedef[/b] struct TestTwo {
TestOne OBJ;
int Z;
} [b]TestTwo[/b];
TestTwo ITEM;
ITEM.OBJ.X = 1;