Public Group

# Problem with SDL class

This topic is 3935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I'm trying to put a few SDL functions to classes. With the entity class i'm trying to create a few circles at different places on the screen and then trying to get them to move. Code: //Entity header file #ifndef ENTITY_H #define ENTITY_H #include <SDL/SDL.h> #include <SDL/SDL_gfxprimitives.h> #include "SDLGraphics.h" class Entity { public: Entity(); int create(SDL_Surface* dest,int,int,int,Uint8 r,Uint8 g,Uint8 b, Uint8 a); int setPosition(int , float); private: int xPos, yPos, size,rad, centreX, centreY; float angle; }; #endif //Entity.cpp file #include <SDL/SDL.h> #include <SDL/SDL_gfxprimitives.h> #include "SDLGraphics.h" #include "Entity.h" int Entity::create(SDL_Surface* dest,int xPos,int yPos,int size,Uint8 r,Uint8 g,Uint8 b, Uint8 a) { filledCircleRGBA( dest, xPos, yPos, size, r, g, b, a); } int Entity::setPosition(int rad, float angle) { return xPos = rad*cos(angle) + centreX; return yPos = rad*sin(angle) + centreY; } This is how i'm trying to use it in main. Code: Entity player; player.setPosition(50,1.57); player.create(screen,xPos,yPos,10,100,100,100,255); I keep on getting an error message saying xPos and yPos are undeclared. I want to be able to create a few objects at different places with independent xPos,yPos and angles which i eventually intend to use to make them move. Thanks.

##### Share on other sites
Well, the error message you gave us really explains exactly what it is. They are undeclared.

Are those members of the class public or private? If they are private, then you will NOT be able to access them directly. Might I suggest Getters and Setters for this.

If they are public, then make sure you put the instance name before the xPos and yPos. Like:

player.create(screen,player.xPos,player.yPos,10,100,100,100,255);

I hope this helps!

##### Share on other sites
Your main function doesn't know what xPos and yPos are because you declared them as private. Making member variables private is a good practice because it hides the implementation, so that if you change the implementation of the Entity class but not the interface (public function declarations), you won't have to change any other code. It also prevents other code from changing the internal details of an Entity, possibly causing problems. So, there are good reasons for having xPos and yPos private, but it prevents your code from working.

I'd suggest you remove xPos and yPos from the declaration of Entity::create(), since these are already set as member variables.
int Entity::create(SDL_Surface* dest,int size,Uint8 r,Uint8 g,Uint8 b, Uint8 a) {     filledCircleRGBA( dest, xPos, yPos, size, r, g, b, a); }

The function still knows what xPos and yPos are because it is a member function of Entity, so it has access to Entity's private variables xPos and yPos.

Also, if a variable is given as an argument to a function, you don't need to declare it as a member variable. Since size, rad, and angle are used only as arguments, they don't need to be declared, and if they are, it can cause confusion since the two variables, while having the same names, do not refer to the same thing (overloading). centreX and centreY, on the other hand, are not given as arguments, so the default value of 0 is used. If this isn't what you want (and it probably isn't, or you could just omit them), you should make centreX and centreY arguments of setPosition().

Another recommendation is to make size, r, g, b, and a all be members of Entity. That's because the size and color of an object are properties of that object, so they should be associated with it. Then create a function which sets these values, like so:
void Entity::setSize(int _size){    size = _size;}

This way, main doesn't have to keep track of the size and color of each Entity. Then when you call Entity::create(), you only need to pass it the dest parameter, and the Entity can take care of the rest.

Thanks.
Got it working.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×