不能声明变量''是抽象类型''

时间:2012-07-25 00:05:53

标签: c++ inheritance gcc abstract-class pure-virtual

编辑:在花了一些时间理解我写的代码后,我仍然不知道它有什么问题。这是我从中派生我的类的基类:

///ContactResultCallback is used to report contact points
struct  ContactResultCallback
{
    short int   m_collisionFilterGroup;
    short int   m_collisionFilterMask;

    ContactResultCallback()
        :m_collisionFilterGroup(btBroadphaseProxy::DefaultFilter),
        m_collisionFilterMask(btBroadphaseProxy::AllFilter)
    {
    }

    virtual ~ContactResultCallback()
    {
    }

    virtual bool needsCollision(btBroadphaseProxy* proxy0) const
    {
        bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0;
        collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask);
        return collides;
    }

    virtual btScalar    addSingleResult(btManifoldPoint& cp,    const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) = 0;
};

现在这是我的派生类:

class DisablePairCollision : public btCollisionWorld::ContactResultCallback
{
public:
    virtual btScalar addSingleResult(btManifoldPoint& cp, const btCollisionObject* colObj0, int32_t partId0, int32_t index0, const btCollisionObject* colObj1, int32_t partId1, int32_t index1);

    btDiscreteDynamicsWorld* DynamicsWorld;
};

以下是我实现主要功能的地方。仍然不确定为什么我会收到此错误。

我在Windows上使用vc2010和代码块的代码没有问题:

btScalar DisablePairCollision::addSingleResult(btManifoldPoint& cp, const btCollisionObject* colObj0, int32_t partId0, int32_t index0, const btCollisionObject* colObj1, int32_t partId1, int32_t index1)
{
    // Create an identity matrix.
    btTransform frame;
    frame.setIdentity();

    // Create a constraint between the two bone shapes which are contacting each other.
    btGeneric6DofConstraint* Constraint;
    Constraint = new btGeneric6DofConstraint( *(btRigidBody*)colObj0, *(btRigidBody*)colObj1, frame, frame, true );

    // Set limits to be limitless.
    Constraint->setLinearLowerLimit( btVector3(1, 1, 1 ) );
    Constraint->setLinearUpperLimit( btVector3(0, 0, 0 ) );
    Constraint->setAngularLowerLimit( btVector3(1, 1, 1 ) );
    Constraint->setAngularUpperLimit( btVector3(0, 0, 0 ) );

    // Add constraint to scene.
    DynamicsWorld->addConstraint(Constraint, true);
    return 0;
}

现在我正在尝试在Ubuntu上编译我的项目,但是当我尝试使用该类时,我收到此错误:

/home/steven/Desktop/ovgl/src/OvglScene.cpp:211: error: cannot declare variable ‘Callback’ to be of abstract type ‘Ovgl::DisablePairCollision’

2 个答案:

答案 0 :(得分:23)

基类是抽象的原因是这个纯虚函数:

virtual btScalar addSingleResult(
  btManifoldPoint& cp,
  const btCollisionObjectWrapper* colObj0Wrap,
  int partId0,
  int index0,
  const btCollisionObjectWrapper* colObj1Wrap,
  int partId1,
  int index1) = 0;

派生类DisablePairCollision尝试定义该函数,但它没有正确执行:

virtual btScalar addSingleResult(
   btManifoldPoint& cp,
   const btCollisionObject* colObj0,
   int32_t partId0,
   int32_t index0,
   const btCollisionObject* colObj1,
   int32_t partId1,
   int32_t index1);

如您所见,某些参数具有不同的类型。例如。 colObj0的类型为const btCollisionObject*,但其类型应为const btCollisionObjectWrapper*

因此,派生类定义具有不同参数的新函数,但它没有从基类定义纯虚函数,因此派生类仍然是抽象的。

您需要使用与基类完全相同的参数类型来定义函数。

答案 1 :(得分:6)

Ovgl::DisablePairCollision是一个抽象类,意味着它包含 至少一个纯虚函数

您需要从此类派生并实现它的抽象功能。

例如:

class DisablePairCollision { virtual void foo() = 0; };
class DerivedClass : public DisablePairCollision { virtual void foo() {} };

DisablePairCollision* pCallback;
pCallback = new DerivedClass;
pCallback->foo();