2014-12-18 81 views
0

我正在用C++進行遊戲。我有一個繼承我的怪物類的玩家和敵人階級。我希望我的敵人類中的構造函數具有與我的怪物類不同的構造函數,但它似乎不起作用。如何覆蓋基類中的構造函數?

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

它似乎不起作用? – 0x499602D2

+1

「這似乎不起作用」,請澄清。 – Borgleader

+0

對不起,編輯我的帖子。 – Wahoozel

回答

1

在Enemy.h你有void update(float delta, Player player);。編譯器如何知道遊戲是什麼?它不能,因爲你從來沒有在enemy.h中包含player.h文件。你必須有選擇來解決這個問題,你可以包括player.h或者你可以forward declare玩家,並改變函數以獲取一個指向玩家的指針。