简体   繁体   English

无法从内部数组或指针向量中访问尖头对象的成员

[英]Unable to access of members of pointed object from inside array or vector of pointers

OK, I have a vector (have also tried an array) of pointers to objects class GameObject- 好的,我有一个向量(也试过一个数组)指向对象类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 并成功填充了指向GameObject派生类对象的指针
(MapObject and PlayerCharacter) (MapObject和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! 谢谢,所有的帮助和时间非常感谢!

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. 你有一个基类GameObject,它有实例变量Pos_x,Pos_y等。然后你用MapObject,PlayerObject将它子类化,它们有自己的同名实例变量。 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. 然后,您可以通过指向GameObject *的指针访问它们,GameObject *只提供对基类实例变量的访问。 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. 定义的类应该只在基类中定义其他字段。

"If you want to compare arbitrary class hierarchies, the safe bet is to make them polymorphic and use dynamic_cast." “如果你想比较任意的类层次结构,安全的做法是使它们具有多态性并使用dynamic_cast。”

See https://stackoverflow.com/a/5662867/912757 请参阅https://stackoverflow.com/a/5662867/912757

try to change for example this statement 尝试改变例如这个陈述

if( prioritycheck == Player_1 ) gameRunning = false;

to

if( dynamic_cast<PlayerCharacter *>( prioritycheck ) == Player_1 )  gameRunning = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM