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

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

本文关键字:访问 对象 成员 内部 向量 指针 数组      更新时间:2023-10-16

好的,我有一个向量(也尝试过一个数组)指向对象类游戏对象的指针-

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

并成功地用指向游戏对象
派生类对象的指针填充了它(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

但是,非常奇怪的是,我似乎无法访问指向的任何对象
的任何成员通过向量/数组中的任何指针,即使我已经验证它们在那里。为
为了调试,我有:

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  

因此,一旦指针存储在向量中,它似乎就知道它是什么,
但不是它的任何成员是什么...有什么想法吗?我似乎无法理解一个宇宙
这个逻辑可以存在的地方...

类定义:

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;
   }
}
};

谢谢,非常感谢您的所有帮助和时间!

你有一个基类游戏对象,它有实例变量Pos_x、Pos_y等。然后,您已经使用MapObject,PlayerObject对其进行了子类化,PlayerObject具有自己的同名实例变量。这导致每个派生类具有两个这样的变量 - (一个用于基,站点用于派生)。

然后,您可以通过指向 GameObject* 的指针访问这些变量,该指针将仅提供对基类实例变量的访问。正如您的测试所示,这些不包含您期望的值。

不需要在基类和子类中声明这些实例变量。在这种情况下,拥有单独的变量似乎没有任何用处;相反,它会阻止您的代码工作。定义的类应仅在基类中的字段之上定义其他字段。

"如果你想比较任意的类层次结构,安全的选择是使它们多态并使用dynamic_cast。

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

尝试更改例如此语句

if( prioritycheck == Player_1 ) gameRunning = false;

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