无法从指针数组或向量内部访问指向对象的成员
Unable to access of members of pointed object from inside array or vector of pointers
好的,我有一个向量(也尝试过一个数组)指向对象类游戏对象的指针-
//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;
相关文章:
- 使用迭代器时如何访问对象在向量中的位置?
- 无法通过指针访问对象的成员
- 如何从传递到对象的对象内部访问对象的类属性?
- 有没有办法在C++中循环访问对象的不同数据成员
- 如何访问对象动态数组中的私有成员变量?
- c++ 循环访问对象列表并删除对象
- 循环访问对象列表 c++
- 访问对象的取消引用值的语法
- 如何访问对象的成员变量的取消引用值
- C++:访问对象内部子对象的方法
- 如果访问对象,则指向的对象内部的指针会更改内存位置
- 是否可以从 C++ 中的构造函数访问对象名称?
- 仅允许访问对象的成员,而不允许访问对象本身
- 从地图访问对象会导致新对象C 的实例化
- 如何在数组中访问对象内的字符串
- 在C 中运行时从指针访问对象实例
- 有关使用矢量迭代器访问对象方法的问题
- 当对象的指针存储在矢量中时,如何访问对象中的方法?
- 显式调用"运算符 new"后无法访问对象的函数
- 尝试访问对象数组中的对象,但只保存了一个对象