3

OK, I have a vector (have also tried an array) of pointers to objects class GameObject-

//GameObject* objPriority_vec [oc+1];
std::vector<GameObject*> objPriority_vec(oc+1);

and have successfully filled it with pointers to objects of derived classes of GameObject
(MapObject and PlayerCharacter)

for(int o_r = 1; o_r <= oc; o_r++)
{
    std::string render_ref = CurrentArea->ObjectRef[o_r];          
    MapObject* render_obj = CurrentArea->ObjectMap[render_ref];

    objPriority_vec[o_r] = render_obj;

}
objPriority_vec[0] = Player_1;    // Player_1 is a pointer to obj   
                                  // PlayerCharacter

But, very strangely, I cannot seem to access any of the members of any of the objects pointed to
by any of the pointers in the vector/array, even though I have verified that they are in there. For
sake of debugging I have:

GameObject* prioritycheck = objPriority_vec[0];  //has been explicitly declared
                                                 //as Player_1 

//When tried one a time  

if( prioritycheck == Player_1 ) gameRunning = false;      //  returns false 
                                                          //(when it should)
if( Player_1->Pos_y > 500) gameRunning = false;           //  returns false 
if( objPriority_vec[0]->Pos_y > 500) gameRunning = false; //  does not 
if( prioritycheck->Pos_y > 500) gameRunning = false;      //  does not  

So once the pointer has been stored in the vector, it seems to know what it is,
but not what any of its members are... any ideas? I can't seem to comprehend a universe
where this logic can exist...

Class Definitions:

class GameObject

{
public:
std::string Graphic_path;
int height;
int width;
int Pos_x;
int Pos_y;
int Vel;
m_dir Dir;
bool moving;
bool colliding;
CollisionBox cCollisionBox;

virtual void Move(){}
virtual void Show(SDL_Surface*, SDL_Surface*){}
virtual void Add(){}
GameObject() {Graphic_path = "NULL"; Pos_x = 0; Pos_y = 0; height = 0; width = 0;}
}; 



class MapObject : public GameObject
{
public:
SDL_Surface* Graphic;
std::string Graphic_path;
int height;
int width;
int Pos_x;
int Pos_y;
int Vel;
m_dir Dir;
bool moving;
bool colliding;
CollisionBox cCollisionBox;


bool SetCollisionBox(int x, int y, int w, int h)
{
cCollisionBox.h = h;
cCollisionBox.w = w;
cCollisionBox.x_off = x;
cCollisionBox.y_off = y;


}
bool CheckCollide(){return false;}


void HandleCollide (int vel_x, int vel_y, int vel, m_dir dir)
{
 switch (dir)
 {

    case Up:
    break;

    case Down:
    break;

    case Left:
    break;

    case Right:
    break;

 }
}

virtual void Move(int v, m_dir dir)
{

 switch (dir)
 {
   case Up:
        Pos_y -= v;
        break;

   case Down:
        Pos_y += v;
        break; 

   case Left:
        Pos_x -= v;
        break;

   case Right:
        Pos_x += v;
        break;     
  }


}

void StandStill()
{
 Vel = 0;
 moving = false;
}



virtual void Show(SDL_Surface*, SDL_Surface*){}
virtual void Add(){}


MapObject()
{
Graphic = NULL; Graphic_path = "NULL"; 
Pos_x = 0; Pos_y = 0; 
height = 0; width = 0;
colliding = false; moving = false;

}


};


class PlayerCharacter : public GameObject
{
public:
int Pos_x;
int Pos_y;
int Vel;
int Vel_x;
int Vel_y;
int frame;
int frame_count;
m_dir Dir;
bool moving;
bool colliding;
SDL_Rect CharClip[20];
CollisionBox cCollisionBox;
void SetClip_Walk()
{
int CharWidth = 22;
int CharHeight = 45;
int CharGap = 6;



    for (int z = 0; z <= 19; z++)
    {
        CharClip[ z ].x = (z * CharWidth) + (z * CharGap);
        CharClip[ z ].y = 0;
        CharClip[ z ].w = CharWidth;
        CharClip[ z ].h = CharHeight;
    }



}

PlayerCharacter()
{
             Pos_x = 0; Pos_y = 0; Vel_x = 0; Vel_y = 0; 
             Vel = 0; moving = false; colliding = false; 
             frame = 0; frame_count = 0;
             Dir = Down;
             SetClip_Walk();
}

bool SetCollisionBox(int x, int y, int w, int h)
{
cCollisionBox.h = h;
cCollisionBox.w = w;
cCollisionBox.x_off = x;
cCollisionBox.y_off = y; 
}

void HandleCollide ()
{

   Pos_y -= (Vel_y);
   Pos_x -= (Vel_x);


   cCollisionBox.x = Pos_x + cCollisionBox.x_off;
   cCollisionBox.y = Pos_y + cCollisionBox.y_off;


}



void Move(int v_x, int v_y, int v, m_dir dir)
{



switch (dir)
{
   case Up:

        Pos_y += v_y;

        break;

   case Down:
        Pos_y += v_y;

        break; 

   case Left:

        Pos_x += v_x;

        break;

   case Right:

        Pos_x += v_x;

        break;     
}


cCollisionBox.x = Pos_x + cCollisionBox.x_off;
cCollisionBox.y = Pos_y + cCollisionBox.y_off;


}

void StandStill()
{
 Vel = 0; Vel_x = 0; Vel_y = 0;
 moving = false;
 colliding = false;
}

void Show(SDL_Surface* source, SDL_Surface* dest)
{

   if (Vel != 0) frame_count++;
   if (frame_count > 3) {frame++; frame_count = 0;}
   if (frame > 4) frame = 1;

   if (Vel == 0) {frame = 0; frame_count = 0;}


   switch (Dir)
   {
          case Up: 
          ApplySurface (Pos_x, Pos_y, source, OutputScreen, &CharClip[frame]);
          break;

          case Down:
          ApplySurface (Pos_x, Pos_y, source, OutputScreen, &CharClip[frame+5]);
          break;

          case Left:
          ApplySurface (Pos_x, Pos_y, source, OutputScreen, &CharClip[frame+10]);
          break;

          case Right:
          ApplySurface (Pos_x, Pos_y, source, OutputScreen, &CharClip[frame+15]);
          break;

   }
}

};

Thank you, all your help and time is greatly appreciated!

Chrysm_Seal
  • 103
  • 8
  • are you trying to access members from the base class or the derived class ? – Moha the almighty camel Jan 11 '14 at 19:36
  • Two questions: 1. Why not just use printf style debugging? Do you not have access to stdout? I would suggest just doing if(...) printf("Condition A is true.\n"); if(...) printf("Condition B is true.\n");, etc. 2. Are you saying that it compiles but gives the wrong results? – CrazyCasta Jan 11 '14 at 19:47
  • @Chrysm_Seal Can you post your class definitions? – CrazyCasta Jan 11 '14 at 19:53
  • @CrazyCasta I understand differently than you. He showed that instead of forthcoming true he gets false. – Vlad from Moscow Jan 11 '14 at 19:55
  • Wow you guys are faster than I though! Yeah Im using SDL and have a window open displaying graphics, no cmdbox or way to do cout or such (havent intergrated displaying txt to sdl output yet either). Will post class definitions 2min. – Chrysm_Seal Jan 11 '14 at 20:28
  • @Mhd.Tahawi: I am trying to access members from the derived class using a pointer to the base class. – Chrysm_Seal Jan 11 '14 at 20:55

3 Answers3

3

You have a base class GameObject which has instance variables Pos_x, Pos_y, etc. You have then subclassed this with MapObject, PlayerObject which have their own instance variables of the same name. This results in each of the derived classes having two of these variables - (one for the base, site for the derived).

You then access these via a pointer to GameObject* which will only provide access to the base class instance variables. As your tests show these do not contain the values you are expecting.

You do not need to declare these instance variables in both base and subclasses. It does not look like having separate variables is serving any purpose in this case; rather it is stopping your code from working. The defined class should only define additional fields on top of those in the base class.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • Just to clarify further, there is almost never a good reason to redefine a variable with the same name in a base class. I'm not going to say there is absolutely never a good reason, but I don't think I've ever come across one. – CrazyCasta Jan 11 '14 at 21:30
  • Oh I see! I did not know this, I thought redefining in the derived class would serve to help with legibility but didn't realise this would actually create another variable of the same name. I will see to this immediately thanks for your help! (oh god I hope this doesn't turn out to be a monster..) – Chrysm_Seal Jan 11 '14 at 22:01
  • Ah you geniuses! (genuii?) deleted and it works with no hassle! (must admit I'm pretty new to this) *kisses each on forehead and skips off into the distance* Thanks muchly! ;) – Chrysm_Seal Jan 11 '14 at 22:07
1

"If you want to compare arbitrary class hierarchies, the safe bet is to make them polymorphic and use dynamic_cast."

See https://stackoverflow.com/a/5662867/912757

Community
  • 1
  • 1
Nil
  • 2,345
  • 1
  • 26
  • 33
  • I thought this might be the problem(well, solution), but I have tried casting both objPriority_vec[0] and GameObject* prioritycheck to no avail, no change in output.. – Chrysm_Seal Jan 11 '14 at 21:43
  • Euh, no, isn't it the other way around? `objPriority_vec[0]` is already a `GameObject*`. You need to cast `Player_1` to the same type the other pointer. In fact, choose one and cast it to the other type :) – Nil Jan 11 '14 at 21:46
  • Ah yes I see! Although after trying this output is still not correct- I think someone else has seen a more likely (or additional) more fundamental problem.. but thanks! – Chrysm_Seal Jan 11 '14 at 21:57
0

try to change for example this statement

if( prioritycheck == Player_1 ) gameRunning = false;

to

if( dynamic_cast<PlayerCharacter *>( prioritycheck ) == Player_1 )  gameRunning = false;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I think the OP is indicating that the prioritycheck does equal Player_1 when it should, that it's the other checks that he is having trouble with. – CrazyCasta Jan 11 '14 at 19:48
  • @CrazyCasta Reading the comment near this line // returns false I do not think so. – Vlad from Moscow Jan 11 '14 at 19:50
  • It says returns false when it should. He's setting a variable to false when they are equal, I think he's saying that when they should be equal that the variable is getting set to false (as it should). – CrazyCasta Jan 11 '14 at 19:52
  • Yeah CrazyCasta has it right, sorry it was probably confusing! Returning false in this context is what it SHOULD do, and it is showing that prioritycheck does indeed equal Player_1 (as it should), but the other checks fail and I cannot understand why.. – Chrysm_Seal Jan 11 '14 at 20:25