C++ 不能将变量 '' 声明为抽象类型 ''
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11641006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot declare variable ‘’ to be of abstract type ‘’
提问by SteveDeFacto
EDIT: After spending a bit of time understanding the code I wrote I still don't know what is wrong with it. This is the base class from which I derived my class:
编辑:在花了一点时间理解我写的代码后,我仍然不知道它有什么问题。这是我派生类的基类:
///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;
};
Now here is my derived class:
现在这是我的派生类:
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;
};
And below is where I implement the main function. Still not sure why I'm getting this error.
下面是我实现主要功能的地方。仍然不确定为什么我会收到此错误。
I was using the code below on windows with both vc2010 and code blocks without a problem:
我在带有 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;
}
Now I'm trying to compile my project on Ubuntu but I am getting this error when I try to use that class:
现在我正在尝试在 Ubuntu 上编译我的项目,但是当我尝试使用该类时出现此错误:
/home/steven/Desktop/ovgl/src/OvglScene.cpp:211: error: cannot declare variable ‘Callback' to be of abstract type ‘Ovgl::DisablePairCollision'
回答by jogojapan
The reason the base class is abstract is this pure virtual function:
基类抽象的原因是这个纯虚函数:
virtual btScalar addSingleResult(
btManifoldPoint& cp,
const btCollisionObjectWrapper* colObj0Wrap,
int partId0,
int index0,
const btCollisionObjectWrapper* colObj1Wrap,
int partId1,
int index1) = 0;
The derived class DisablePairCollision
attempts to define that function, but it does not do it correctly:
派生类DisablePairCollision
尝试定义该函数,但它没有正确定义:
virtual btScalar addSingleResult(
btManifoldPoint& cp,
const btCollisionObject* colObj0,
int32_t partId0,
int32_t index0,
const btCollisionObject* colObj1,
int32_t partId1,
int32_t index1);
As you can see, some of the arguments have different types. E.g. colObj0
is of type const btCollisionObject*
, but it should be of type const btCollisionObjectWrapper*
.
如您所见,一些参数具有不同的类型。例如colObj0
是类型const btCollisionObject*
,但它应该是类型const btCollisionObjectWrapper*
。
Therefore, the derived class defines a new function with different arguments, but it does not define the pure virtual function from the base class, hence the derived class is still abstract.
因此,派生类定义了一个具有不同参数的新函数,但它没有定义基类的纯虚函数,因此派生类仍然是抽象的。
You need to define the function with the exact same argument types as in the base class.
您需要使用与基类中完全相同的参数类型来定义函数。
回答by Aesthete
Ovgl::DisablePairCollision
is an abstract class, meaning it contains at least one pure virtual function.
Ovgl::DisablePairCollision
是一个抽象类,这意味着它至少包含一个纯虚函数。
You need to derive from this class and implement the abstractfunctionality of it.
您需要从这个类派生并实现它的抽象功能。
For example:
例如:
class DisablePairCollision { virtual void foo() = 0; };
class DerivedClass : public DisablePairCollision { virtual void foo() {} };
DisablePairCollision* pCallback;
pCallback = new DerivedClass;
pCallback->foo();