【发布时间】: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::Drawable、sf::Transformable或sf::NonCopyable中的任何一个是否具有您没有覆盖的纯虚函数? -
回答我自己的问题,是的,是的:sfml-dev.org/documentation/2.0/classsf_1_1Drawable.php
-
@clcto 是的 - 都是 SFML 类,Drawable 需要一个 Draw()
标签: c++ object c++11 abstract sfml