|
-
February 13th, 2008, 05:39 AM
#1
Abstract class and vector issue
I have an abstract class, SceneObject declared as follow:
PHP Code:
#ifndef SCENE_OBJECT_H
#define SCENE_OBJECT_H
#include "Application.h"
class SceneObject{
public:
SceneObject(void);
virtual ~SceneObject(void);
public:
virtual void update(Application* app)=0;
virtual void render(Application* app)=0;
};
#endif // SCENE_OBJECT_H
There are two more that derive from the above, an implementation...
PHP Code:
#ifndef GROUP_H
#define GROUP_H
#include <vector>
#include "SceneObject.h"
class Group : public SceneObject{
private:
std::vector<SceneObject> children;
public:
Group(void);
~Group(void);
public:
void update(Application* app);
void render(Application* app);
};
#endif // GROUP_H
... and a supposed more specialized abstract class:
PHP Code:
#ifndef ACTOR_H
#define ACTOR_H
#include "SceneObject.h"
class Actor : SceneObject{
public:
Actor(void);
~Actor(void);
public:
virtual void update(Application* app)=0;
virtual void render(Application* app)=0;
};
#endif // ACTOR_H
As you can see, Group has a vector of SceneObjects that will eventually contain Groups or Actors.
On compile, the compiler yields
Code:
c:\program files\microsoft visual studio 9.0\vc\include\vector(716) : error C2259: 'SceneObject' : cannot instantiate abstract class
due to following members:
'void SceneObject::update(Application *)' : is abstract
c:\documents and settings\alexandre\my documents\visual studio 2008\projects\ocaso\ocaso\sceneobject.h(13) : see declaration of 'SceneObject::update'
'void SceneObject::render(Application *)' : is abstract
c:\documents and settings\alexandre\my documents\visual studio 2008\projects\ocaso\ocaso\sceneobject.h(14) : see declaration of 'SceneObject::render'
c:\documents and settings\alexandre\my documents\visual studio 2008\projects\ocaso\ocaso\group.h(10) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
with
[
_Ty=SceneObject
]
Any help is welcome.
Last edited by alexin; February 13th, 2008 at 01:58 PM.
Reason: Correction
-
February 13th, 2008, 06:07 AM
#2
Re: Abstract class and vector issue
As your SceneObject is an abstract class the compiler cannot create a vector of them in the line std::vector<SceneObject> children;
What you need is
std::vector<SceneObject *> children;
Even if your base class wasn't abstract, the original vector would still not have worked as any derived classes of SceneObject added to the vector would have been 'sliced' (All extra information declared in the derived object would be lost as each would be up-converted to SceneObject)
-
February 13th, 2008, 06:52 AM
#3
Re: Abstract class and vector issue
So I'll be getting a pointer to SceneObject with the possibility to down-cast it...
I think I'll probably run into more problems but it's fine.
Thank you!
-
February 13th, 2008, 07:02 AM
#4
Re: Abstract class and vector issue
You don't need to downcast if you're only using the overridden update and render functions.
Code:
children[i]->update(...);
This will call the correct overridden function for the derived class.
-
February 13th, 2008, 07:07 AM
#5
Re: Abstract class and vector issue
I assume the code for Group will of the form...
Code:
void Group::render(Application* app)
{
std::vector<SceneObject *>::iterator itr = children.begin();
while (itr != children.end())
{
(*itr)->render(app);
++itr;
}
}
-
February 13th, 2008, 11:08 AM
#6
Re: Abstract class and vector issue
Also note that you've got class Actor deriving privately from SceneObject, so it can't be pointed to by a SceneObject pointer. You probably want public derivation.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 13th, 2008, 01:19 PM
#7
Re: Abstract class and vector issue
 Originally Posted by JohnW@Wessex
You don't need to downcast if you're only using the overridden update and render functions.
I'm aware, thanks 
 Originally Posted by Graham
Also note that you've got class Actor deriving privately from SceneObject, so it can't be pointed to by a SceneObject pointer. You probably want public derivation.
I don't see what you mean by that...
-
February 13th, 2008, 01:32 PM
#8
Re: Abstract class and vector issue
Code:
class Actor : SceneObject{
classes derive privately by default, so the above is equivalent to
Code:
class Actor : private SceneObject{
Private derivation is not visible outside of the (derived) class, so external code cannot assign a pointer to Actor to a pointer to SceneObject, therefore, you can't store an actor in your vector<SceneObject*>.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 13th, 2008, 01:41 PM
#9
Re: Abstract class and vector issue
Oh, I didn't know about that.
Thanks for the note, you've probably saved me some headaches.
EDIT:
I've applied the suggested modifications, you guys told me, but I've another problem now. the compilation is successful but when linking...
Code:
Actor.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall SceneObject::~SceneObject(void)" (??1SceneObject@@UAE@XZ) referenced in function "public: virtual __thiscall Actor::~Actor(void)" (??1Actor@@UAE@XZ)
Group.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall SceneObject::~SceneObject(void)" (??1SceneObject@@UAE@XZ)
SceneObject.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall SceneObject::~SceneObject(void)" (??1SceneObject@@UAE@XZ)
Note: The Group class declaration misses a ': public SceneObject'; my code is corrected.
Last edited by alexin; February 13th, 2008 at 01:56 PM.
-
February 14th, 2008, 03:42 AM
#10
Re: Abstract class and vector issue
Looks like you never created a definition for the SceneObject destructor. As SceneObject is abstract I'm pretty sure you can define it as a pure virtual, as long as a destructor is defined in the derived class.
-
February 14th, 2008, 04:42 AM
#11
Re: Abstract class and vector issue
You can declare a destructor as pure virtual, but you have to give it a definition - you can't get away with not defining a destructor that you've declared - that's one function that will definitely be called. In this case, however, I would suggest that simple inline blank destructor is all that's needed:
Code:
class SceneObject
{
public:
virtual ~SceneObject() {}
//...
};
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 14th, 2008, 09:35 AM
#12
Re: Abstract class and vector issue
Then subclasses can decide either to define their own destructor or not.
Thanks guys!
-
February 14th, 2008, 10:14 AM
#13
Re: Abstract class and vector issue
Subclass doesnt need an destructor if it has got no business...
BUt its always better to give one destructor even if its a blank..
like ~SubClass(){};
Dont forget to rate my post if you find it useful.
-
February 14th, 2008, 10:21 AM
#14
Re: Abstract class and vector issue
BUt its always better to give one destructor even if its a blank..
like ~SubClass(){};
What's your rationale for that?
-
February 14th, 2008, 10:41 AM
#15
Re: Abstract class and vector issue
It just gives out an impression that class creator has not forgotten to implement destructor.. He has not missed it...
If I dont see a destructor I would wonder whether he forgot to implement one...???
Dont forget to rate my post if you find it useful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|