好的,我有一个指向对象类 GameObject 的指针 vector (也尝试过一个数组)-
//GameObject* objPriority_vec [oc+1];
std::vector<GameObject*> objPriority_vec(oc+1);
并成功地用指向 GameObject 派生类的对象的指针填充它
( map 对象和玩家角色)
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
但是,非常奇怪的是,我似乎无法访问所指向的任何对象的任何成员
通过 vector/数组中的任何指针,即使我已经验证它们在那里。对于
为了调试我有:
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
所以一旦指针被存储在vector中,它似乎就知道它是什么了,
但不是它的任何成员......有什么想法吗?我似乎无法理解一个宇宙
这个逻辑可以存在的地方......
类定义:
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;
}
}
};
谢谢,非常感谢您的帮助和时间!
最佳答案
您有一个基类 GameObject,它具有实例变量 Pos_x、Pos_y 等。然后您将其子类化为 MapObject、PlayerObject,它们具有自己的同名实例变量。这导致每个派生类都有两个这样的变量——(一个用于基础,站点用于派生)。
然后您可以通过指向 GameObject* 的指针访问这些变量,这将只提供对基类实例变量的访问。如您的测试所示,这些不包含您期望的值。
您不需要在基类和子类中都声明这些实例变量。在这种情况下,单独的变量看起来并没有起到任何作用;相反,它会阻止您的代码工作。定义的类应该只在基类中的那些之上定义额外的字段。
关于c++ - 无法从数组或指针 vector 内部访问指向对象的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066868/