-
C++ compile problem
I have a compile problem here.
I have a vector declared as:
Code:
std::vector<CGameObject> _vGameObjects;
CGameObject is an abstract class.
Code:
_vGameObjects.push_back(_paddle);
In the code above I want to push a _paddle object, which is an instance of a concrete object derived from the CGameObject.
The compiler (VC++ 6.0) generates an error and a series of warnings:
c:\program files\microsoft visual studio\vc98\include\xmemory(34) : error C2259: 'CGameObject' : cannot instantiate abstract class due to following members:
f:\projects\simple game\game1\src\cgameobject.h(33) : see declaration of 'CGameObject'
c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CGameObject *,const class CGameObject &)' being compiled
c:\program files\microsoft visual studio\vc98\include\xmemory(34) : warning C4259: 'void __thiscall CGameObject::InitBoundingBoxes(void)' : pure virtual function was not defined
f:\projects\simple game\game1\src\cgameobject.h(51) : see declaration of 'InitBoundingBoxes'
c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CGameObject *,const class CGameObject &)' being compiled
c:\program files\microsoft visual studio\vc98\include\xmemory(34) : warning C4259: 'class std::vector<struct BBox,class std::allocator<struct BBox> > &__thiscall CGameObject::BoundingBoxes(void)' : pure virtual function was not defined
f:\projects\simple game\game1\src\cgameobject.h(59) : see declaration of 'BoundingBoxes'
c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CGameObject *,const class CGameObject &)' being compiled
c:\program files\microsoft visual studio\vc98\include\xmemory(34) : error C2259: 'CGameObject' : cannot instantiate abstract class due to following members:
f:\projects\simple game\game1\src\cgameobject.h(33) : see declaration of 'CGameObject'
c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CGameObject *,const class CGameObject &)' being compiled
c:\program files\microsoft visual studio\vc98\include\xmemory(34) : warning C4259: 'void __thiscall CGameObject::InitBoundingBoxes(void)' : pure virtual function was not defined
f:\projects\simple game\game1\src\cgameobject.h(51) : see declaration of 'InitBoundingBoxes'
c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CGameObject *,const class CGameObject &)' being compiled
c:\program files\microsoft visual studio\vc98\include\xmemory(34) : warning C4259: 'class std::vector<struct BBox,class std::allocator<struct BBox> > &__thiscall CGameObject::BoundingBoxes(void)' : pure virtual function was not defined
f:\projects\simple game\game1\src\cgameobject.h(59) : see declaration of 'BoundingBoxes'
c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CGameObject *,const class CGameObject &)' being compiled
Any suggestions? Thanks!
-
Make sure you define all your abstract methods somewhere, and you could use a vector of pointers to CGameObject rather than the objects themeselves as the vector implementation is probably trying to call the abstract constructor.
-
You cannot create an abstract calss object. Use pointers as Sorensen suggested
std::vector<CGameObject*> _vGameObjects;
_vGameObjects->push_back(_paddle);
-
Thanks to both of you!
I was wondering if I could use references instead of pointers. Is there any significative difference?
... on a second thought, it does not compile if I try using references... the pointer solution just works :)