C++ structs

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Sachiel7
Posts: 21
Joined: Fri Dec 30, 2005 6:24 pm
Location: USA

C++ structs

Post 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?
-=Sachiel7=-
TreyOne Games
Posts: 18
Joined: Wed Dec 14, 2005 12:44 am

Post by TreyOne Games »

maybe this can help u understand better :?

http://www.fredosaurus.com/notes-cpp/st ... ructs.html
TreyOne Games
Posts: 18
Joined: Wed Dec 14, 2005 12:44 am

Post by TreyOne Games »

jam
Posts: 409
Joined: Fri Nov 04, 2005 3:52 am

Post 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;
}
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

hmm my money is on "ITEM.OBJ.X = 1;" being outside main() ;)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Sachiel7
Posts: 21
Joined: Fri Dec 30, 2005 6:24 pm
Location: USA

Post 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?
-=Sachiel7=-
Sachiel7
Posts: 21
Joined: Fri Dec 30, 2005 6:24 pm
Location: USA

Post 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!
-=Sachiel7=-
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post 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
  }
}
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Guest

Post 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 };
Guest

Post 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;
Post Reply