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

C++ class with pointer to struct

Postby Alpha Omega » Sun Jan 09, 2011 12:34 am

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]
User avatar
Alpha Omega
 
Posts: 284
Joined: Wed Oct 29, 2008 12:07 pm

Postby Lonesome Ducky » Sun Jan 09, 2011 2:34 am

...Are you sure? I copied that exact code and it works perfectly fine in MSVC++ 2008
User avatar
Lonesome Ducky
Competition winner
 
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Postby d3jake » Sun Jan 09, 2011 4:26 am

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
User avatar
d3jake
 
Posts: 198
Joined: Sat Mar 22, 2008 7:49 pm
Location: United States of America

Postby Lonesome Ducky » Sun Jan 09, 2011 6:22 am

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 .
User avatar
Lonesome Ducky
Competition winner
 
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Postby Alpha Omega » Sun Jan 09, 2011 3:45 pm

Yea its weird it wasnt working and then I just booted it up this morning and it worked fine :(.
User avatar
Alpha Omega
 
Posts: 284
Joined: Wed Oct 29, 2008 12:07 pm


Return to Game Programming

Who is online

Users browsing this forum: No registered users and 1 guest