I have class "Bullet" with x and y declared as public variables.
I need to create objects of the class within the program so I tried using a pointer:
Bullet *aBullet = new Bullet(ship.x, ship.y + 20, 10);
When trying to call the y variable (Bullet::bBullet.y <<< not sure if this is right) I get an error saying aBullet isn't a member of Bullet.
Any help would be appreciated.
Accessing class object member pointer variables
Moderator: Coders of Rage
-
- Chaos Rift Newbie
- Posts: 14
- Joined: Wed Apr 15, 2009 6:40 pm
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Accessing class object member pointer variables
just do aBullet.y. Bullet::aBullet.y is saying to the compiler "get the static member of Bullet named aBullet, then get its member y", when what you want is just "get aBullet's member that is named y".
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
-
- Chaos Rift Newbie
- Posts: 14
- Joined: Wed Apr 15, 2009 6:40 pm
Re: Accessing class object member pointer variables
Thanks for the reply
Thats what I originally had, but it gave the error "request for member 'y' in 'aBullet', which is of non-class type 'Bullet*'
Thats what I originally had, but it gave the error "request for member 'y' in 'aBullet', which is of non-class type 'Bullet*'
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Accessing class object member pointer variables
well then you did something wrong in creating the Bullet class. Mind posting some code?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
-
- Chaos Rift Newbie
- Posts: 14
- Joined: Wed Apr 15, 2009 6:40 pm
Re: Accessing class object member pointer variables
If you need to do something extra for using the pointer then that's my problem.
Thanks for the help
Code: Select all
class Bullet {
public:
float x;
float y;
float radius;
void bDisplay();
Bullet(float, float, float);
};
Bullet::Bullet(float xx, float yy, float rr) {
x = xx;
y = yy;
radius = rr;
}
void Bullet::bDisplay() {
glBegin(GL_LINE_LOOP);
glVertex3f(x-(radius*0.5), y-(radius*0.5), 0.0f);
glVertex3f(x+(radius*0.5), y-(radius*0.5), 0.0f);
glVertex3f(x+(radius*0.5), y+(radius*0.5), 0.0f);
glVertex3f(x-(radius*0.5), y+(radius*0.5), 0.0f);
glEnd();
}
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Accessing class object member pointer variables
oh wait, you need to use -> instead of . because aBullet is a pointer. a->b is shorthand for (*a).b, so it dereferences the pointer for you.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
-
- Chaos Rift Newbie
- Posts: 14
- Joined: Wed Apr 15, 2009 6:40 pm
Re: Accessing class object member pointer variables
Ohh that makes sense. Thanks again for you help.