我正在尝试使指针指向某个向量,但在函数结束后,调试器显示指针不再指向该向量。相关代码如下:
#pragma once
#include "Configuration.h"
#include "Enemy.h"
class FireBall:public sf::Sprite{
public:
FireBall(const sf::Vector2f position, Tile * startTile);
void moveFB();
void setTrail(vector<Tile *> *trail);
void removeTrail();
private:
sf::Vector2f _position;
vector<Tile *> *_p2_trail;
vector<Tile *>::iterator _it2Tile;
sf::Clock _clock;
int _lastMoveTime;
float getDecimalPart(float number);
};
void FireBall::setTrail(vector<Tile *> *trail){
_p2_trail = trail; // here it points to the vector (SHOULED REMAIN LIKE THAT AFTER FUNCTION ENDS....)
_it2Tile = _p2_trail->begin(); // setting iterator to begin
}
void GameBoard::setFireBall(bool set, float x, float y){
if (set)
_p2FB = new FireBall(sf::Vector2f((float)x*OFFSET,(float)y*OFFSET), _gameBoard[x][y]);
else{
_p2FB->removeTrail();
delete _p2FB;
}
_hasFireBall = set;
_p2FB->setTrail(&_player->getTrail());
// at this point _p2FB no more points to the vecotr! why?!!
}
播放器标题:#pragma once #include "Configuration.h" #include "Character.h"
class Player:public Character{
public:
Player(vector< vector <Tile *>> &gameBoard, const sf::Vector2f position, const IMAGE img);
bool isLegalMove(const sf::Vector2f &position);
void move(sf::Vector2f &direction, Tile ** previousTile, Tile ** currentTile);
void act();
void setShielded(const bool shieldMode){ _sheilded = shieldMode; };
bool getShielded() const {return _sheilded; };
vector<Tile *> getTrail(){ return _trail; };
void addToTrail(Tile * tile);
void convertTrailToShield();
void removeTrail();
void fixBoardPosition();
void updateShielded(const pair<int,int>& prevPosition);
private:
bool _sheilded;
vector<Tile *> _trail;
};