15

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

///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;
};

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

我在带有 vc2010 和代码块的 windows 上使用下面的代码没有问题:

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’
4

2 回答 2

31

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

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);

如您所见,一些参数具有不同的类型。egcolObj0是 type const btCollisionObject*,但它应该是 type const btCollisionObjectWrapper*

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

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

于 2012-07-25T01:41:36.860 回答
7

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

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

例如:

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

DisablePairCollision* pCallback;
pCallback = new DerivedClass;
pCallback->foo(); 
于 2012-07-25T00:35:18.410 回答