好吧...我已經理解了關於循環依賴和前向聲明的這個問題,但是我無法理解涉及繼承和基於指針變量的特定錯誤。子類中使用的不完整指針 - 錯誤:使用未定義類型
我會顯示相關的代碼片段:
Player是實體派生類
Entity.hpp
class Entity : public sf::Sprite{
private:
int health;
float speed;
sf::Time spawntime;
bool invincible;
protected:
SceneGame *myScene; // let's keep it at null
// since setScene will take care of it
public:
// more code....
void setScene(SceneGame *scene){myScene = scene;};
SceneGame* getScene(){return myScene;};
};
player.cpp //假設player.h完成
myScene可以從派生形式的任何類訪問實體
void Player::shootPlayer(float dt){
// we reduce time for shoot delay
shootDelay -= dt;
if (shootDelay < 0.0f) return;
resetDelay();
if (Input::instance()->pressKeybutton(sf::Keyboard::Space)){
// I know I set SceneGame* myScene as protected back in Entity class.
// However, because it is claimed to be undefined,
// despite being forward-declared back in entity,
// I'm getting the 'use of undefined type class' error C2027
sf::Texture bulletTex;
bulletTex = (myScene->game->texmgr.getRef("bulletPlayer"));
Bullet* bullet_p = new Bullet(bulletTex,
1, 10, false, false, 0.0f);
bullet_p->setPosition(this->getGlobalBounds().width,
this->getGlobalBounds().height/2);
}
}
鏈接,回答問題: Forward declaration & circular dependency
謝謝......我意識到我不需要在播放器文件中進行前向聲明。我只需要包含。 前向聲明僅當我必須表示某個特定類的變量時纔有用。 – JBRPG 2015-02-24 00:17:35