如何覆盖基类中的构造函数?

时间:2014-12-18 19:39:50

标签: c++

我正在用C ++制作游戏。我有一个继承我的暴徒类的玩家和敌人类。我希望我的敌人类中的构造函数具有与我的mob类不同的构造函数,但它似乎不起作用。

Mob.h

#pragma once

#include <SFML/Graphics.hpp>

class Mob
{
public:
     Mob();
    ~Mob();

    void update();
    void render();

protected:
    static sf::Sprite mSprite;

    static float mSpeed;
};

Mob.cpp

#include <SFML/Graphics.hpp>
#include "Enemy.h"
#include "Player.h"

Mob::Mob()
{

}

Mob::~Mob() {

}

Enemy.h

#pragma once

#include <SFML/Graphics.hpp>
#include "Mob.h"

class Enemy : public Mob
{
public:
    Enemy(sf::Sprite sprite);
    ~Enemy();

    void update(float delta, Player player);
    void render(sf::RenderWindow& window);
};

Enemy.cpp

#include <SFML/Graphics.hpp>
#include "Enemy.h"
#include "Player.h"

Enemy::Enemy(sf::Sprite sprite)
{
    mSprite = sprite;

    mSpeed = 150.0f;
}

Enemy::~Enemy() {

}

void Enemy::update(float delta, Player player) {

}

void Enemy::render(sf::RenderWindow& window) {
    window.draw(mSprite);
}

Player.h

#pragma once

#include <SFML/Graphics.hpp>
#include "Mob.h"

class Player : public Mob
{
public:
    Player(sf::Sprite sprite);
    ~Player();

    void update(float delta);
    void render(sf::RenderWindow& window);
};

Player.cpp

#include <SFML/Graphics.hpp>
#include "Player.h"

Player::Player(sf::Sprite sprite)
{
    mSprite = sprite;

    mSpeed = 150.0f;
}

Player::~Player() {

}

void Player::update(float delta) {

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        mSprite.move(0, -mSpeed * delta);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        mSprite.move(-mSpeed * delta, 0);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        mSprite.move(0, mSpeed * delta);
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        mSprite.move(mSpeed * delta, 0);
    }
}

void Player::render(sf::RenderWindow& window) {
    window.draw(mSprite);
}

忘记添加我的错误:

Error   3   error C2511: 'void Enemy::update(float,Player)' : overloaded member function not found in 'Enemy'   c:\dev\c++\sven\sven\enemy.cpp  16  1   Sven
Error   1   error C2061: syntax error : identifier 'Player' c:\dev\c++\sven\sven\enemy.h    12  1   Sven
Error   2   error C2061: syntax error : identifier 'Player' c:\dev\c++\sven\sven\enemy.h    12  1   Sven

1 个答案:

答案 0 :(得分:1)

在Enemy.h中你有void update(float delta, Player player);。编译器如何知道游戏是什么?因为你从未在enemy.h中包含player.h文件,所以它无法使用。您必须选择修复此问题,您可以包含player.h或者您可以forward declare播放器并更改函数以获取指向播放器的指针。