名为terminate的纯虚方法在没有活动异常的情况下调用

时间:2016-12-16 17:14:26

标签: c++ sfml virtual-functions

我有一个问题。我正在制作一个游戏,我检查一个抛射物是否在屏幕下方,如果是,我想要删除该对象。我的问题是,当射弹实际上低于场景时,我的游戏会因错误而崩溃:

  

名为terminate的纯虚方法在没有活动的情况下调用   例外

我觉得非常奇怪,因为我的抛射类与虚方法或抽象类无关。这是更新方法:

void GameScene::updateLogic() {
    if (projectile) {
        if (projectile -> position().y > 1200) {
            t1Turn = true;
            delete projectile;
        }
        else {
            projectile->update();
        }
    }
}

void GameScene::draw() {
    if (projectile) {
        projectile->draw(_window);
    }
}

射弹指针在gamescene的构造函数中设置为nullptr:

_p = nullptr;

射弹类:

Projectile::Projectile(sf::Vector2f spawnPoint, sf::Vector2f initVel, Tank* target, bool& turn) : _spawnPoint(spawnPoint), _initVel(initVel), _target(target), _turn(turn) {}
Projectile::~Projectile(){}
void Projectile::update(){
   if (_collisionBox.intersects(_target->getBox())) {
       _target->removeHealth(20);
    }
    _elapsed = clock.getElapsedTime().asSeconds();
    _time += _elapsed;
    clock.restart();
    dx = (_initVel.x * _time);
    dy = (_initVel.y * _time + (1900*pow(_time, 2) / 2));
    _position = sf::Vector2f(_spawnPoint.x+dx, _spawnPoint.y+dy);
    _collisionBox = _projectileSprite.getGlobalBounds();
}

void Projectile::draw(sf::RenderWindow &window) {
    _projectileSprite.setPosition(_position);
    window.draw(_projectileSprite);
}

sf::Vector2f Projectile::position () {
    return _position;
}

抛射物继承自精灵类,但就此而言:

class Projectile : Sprites {

它与纯虚方法无关......

编辑:运行调试器时,它会断开:

void Projectile::draw(sf::RenderWindow &window) { window <incomplete type>
    _projectileSprite.setPosition(_position);
    window.draw(_projectileSprite); <--------------------
}

但窗口怎么可能是不完整的类型?与原始错误消息所说的虚拟方法有什么关系?

0 个答案:

没有答案