2015-02-23 61 views
1

好吧...我已经理解了关于循环依赖和前向声明的这个问题,但是我无法理解涉及继承和基于指针变量的特定错误。子类中使用的不完整指针 - 错误:使用未定义类型

我会显示相关的代码片段:

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

回答

1

您有SceneGame的前向声明,可能是entity.hpp的形式为class SceneGame;。这足以将其用作指针。

player.cpp,你实际上使用这个类,你需要知道它的细节(你不需要在标题中)。可能的是,你的player.cpp应包括在结尾处增加一个#include "SceneGame.hpp"SceneGame.hpp(或任何你SceneGame类实际上定义)

修复在你的player.cpp文件中包含。

+0

谢谢......我意识到我不需要在播放器文件中进行前向声明。我只需要包含。 前向声明仅当我必须表示某个特定类的变量时才有用。 – JBRPG 2015-02-24 00:17:35

0

虽然你已经向前声明的SceneGame类,你包括完整的定义,你开始使用这个类的任何方法之前,否则,编译器如何知道SceneGame类支持哪些方法?

我想你需要#include "SceneGame.h"或类似的在你的Player.cpp文件。

相关问题