C++ class with pointer to struct

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
Alpha Omega
Posts: 288
Joined: Wed Oct 29, 2008 12:07 pm

C++ class with pointer to struct

Post by Alpha Omega »

Hey I am writing some code with Irrlicht and want to make a class with a pointer to struct class member. Such as

Code: Select all


struct thing1
{
     int a;
};

class Thing
{
      public:
      thing1* b;
};


I thought that to access the struct part of the class it would be

Code: Select all


Thing* apple;

apple->b->a;

But I get a compiler error saying that this "a" is not a member of b. Is this a special rule in c++?[/code]
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

...Are you sure? I copied that exact code and it works perfectly fine in MSVC++ 2008
d3jake
Posts: 198
Joined: Sat Mar 22, 2008 7:49 pm
Location: United States of America

Post by d3jake »

I think the error may lie in the fact that a, within the struct is simply an object of type int, it is not a pointer. My guess would be that the correct line of code to access it would be:

Code: Select all

apple->b.a
I seem to remember getting caught up with something like this in the past. The compiler error is one of those that give you half an idea as to what the actual problem is.
The Open Descent Foundation is always looking for programmers! http://www.odf-online.org
"I'll find out if what I deleted was vital here shortly..." -d3jake
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

d3jake wrote:I think the error may lie in the fact that a, within the struct is simply an object of type int, it is not a pointer. My guess would be that the correct line of code to access it would be:

Code: Select all

apple->b.a
I seem to remember getting caught up with something like this in the past. The compiler error is one of those that give you half an idea as to what the actual problem is.
Nope. Though a is not a pointer b IS. And to access the members of a pointer you have to use ->. If you were somehow accessing the members of a instead, then you would use .
Alpha Omega
Posts: 288
Joined: Wed Oct 29, 2008 12:07 pm

Post by Alpha Omega »

Yea its weird it wasnt working and then I just booted it up this morning and it worked fine :(.
Post Reply