【问题标题】:Allocating an object of abstract class type when trying to create a SceneNode尝试创建 SceneNode 时分配抽象类类型的对象
【发布时间】:2014-11-18 00:52:26
【问题描述】:

我有一个问题在 C++ 中无法解决。

我有一个名为 SceneNode 的类。在这个类中,没有任何虚函数,在私有成员中,我有一个 unique_ptr 向量和一个指向 SceneNode 对象的原始指针。 尝试分配新的 SceneNode 时,出现以下错误:分配抽象类类型“SceneNode”的对象。

代码如下:

    class SceneNode : public  sf::Drawable,
                  public  sf::Transformable,
                  private sf::NonCopyable
{

    //OVERVIEW: A SceneNode is a node from the scene graph. It represents a graphical element
    //A typical SceneNode is (PARENT, CHILDREN, TRANSFORM)
    //With TRANSFORM containing several information:
    //TRANSFORM.POS = the position of this
    //TRANSFORM.ROTATION = the rotation of this

    //The transformation of this is always relative to its parent
    //Therefore, TRANSFORM.POS is the position of this, relatively to its parent


    //NB: - a SceneNode is not copyable !
    //    - It's an abstract class

public:

    //--------------------------------------------
    //Typedefs
    //--------------------------------------------
    typedef std::unique_ptr<SceneNode> UniquePtr;
    typedef sf::Vector2f               Position;

public:

    //--------------------------------------------
    //Constructors
    //--------------------------------------------
    SceneNode();
        //REQUIRES: /
        //MODIFIES: this
        //EFFECTS: initializes this with this_post.PARENT = no parent
        //         and this.CHILDREN = { ⦰ }

public:

    //--------------------------------------------
    //Public member functions
    //--------------------------------------------
    void attachChild(UniquePtr child);
        //REQUIRES: child != nullptr
        //MODIFIES: this
        //EFFECTS: if child == nullptr, stops the program;
        //         else, this_post.CHILDREN = this.CHILDREN U { child }

    UniquePtr detachChild(const SceneNode& child);
        //REQUIRES: /
        //MODIFIES: this
        //EFFECTS: if child in this.CHILDREN, this_post.CHILDREN = this.CHILDREN \ child && returns a unique_ptr to the child, don't catch it to let it being freed

    sf::Transform getWorldTransform() const;
        //REQUIRES: /
        //MODIFIES: /
        //EFFECTS: returns the absolute transformation of this

    Position getWorldPosition() const;
        //REQUIRES: /
        //MODIFIES: /
        //EFFECTS: returns the absolute position of this



private:

    //--------------------------------------------
    //Representation
    //--------------------------------------------
    SceneNode*             mParent;
    std::vector<UniquePtr> mChildren;



};`

我能做什么?提前致谢

【问题讨论】:

  • sf::Drawablesf::Transformablesf::NonCopyable 中的任何一个是否具有您没有覆盖的纯虚函数?
  • 回答我自己的问题,是的,是的:sfml-dev.org/documentation/2.0/classsf_1_1Drawable.php
  • @clcto 是的 - 都是 SFML 类,Drawable 需要一个 Draw()

标签: c++ object c++11 abstract sfml


【解决方案1】:

您似乎是从sf::Drawable 之类的抽象接口继承而来,但没有实现它们定义的纯虚函数(在 Drawable 的情况下为 draw() 函数)。如果你在你的类中实现这些函数,你应该摆脱编译器错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多