c++ - 访问指针 vector 中的元素

标签 c++ pointers vector

这可能很简单,但我找不到:

我想访问指针 vector 中的一个元素

 class Tile
  {
  public:
    Tile(int xPosition, int yPosition, float tileWeight);
    float getValue() const {return value;};
    void setValue(float newValue) {value = newValue;};
    int getXPos() const {return xPos;};
    int getYPos() const {return yPos;};
    void setXPos(int newPos) {xPos = newPos;};
    void setYPos(int newPos) {yPos = newPos;}

  private:
    int xPos;
    int yPos;
    float value;
  };

class Enemy : public Tile
  {
  public:
    Enemy(int xPosition, int yPosition, float strength);
  };

在另一个类(class)

std::vector<Enemy*> enemies;
enemies = myWorld->getEnemies(5);

我怎样才能访问另一个类中第一个成员的值 我似乎无法在另一个类(class)访问它

MySquare::movePlayer(std::vector <int> directions, std::vector<Enemy*> enemies,std::vector<int*> healthPks){

}

最佳答案

例如,要在 vector 中的第一项上调用 getValue 函数,请使用

enemies[0]->getValue();

关于c++ - 访问指针 vector 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14186176/

相关文章:

c++ - 我不明白 map 插入时的这个错误

c++ - 使用 GDB 在 Windows 下调试 DLL

C 指针不一致

c++ - QSharedPointer 不返回 false

r - 如何从向量或列表中消除 n 个连续值?

c++ - 来自 C++ 的中间代码

c++ - 使用 XSendEvent 传递鼠标事件

c++ - 这个地址

c++ - 在目录 C++ 中搜索文件的函数输出错误

c++ - 如何从 vector C++ 中的字符串指向第 n 个字符