CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Jan 2008
    Posts
    78

    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(Applicationapp)=0;
        
    virtual void render(Applicationapp)=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<SceneObjectchildren;

    public:
        
    Group(void);
        ~
    Group(void);

    public:
        
    void update(Applicationapp);
        
    void render(Applicationapp);
    };
    #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(Applicationapp)=0;
        
    virtual void render(Applicationapp)=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

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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)

  3. #3
    Join Date
    Jan 2008
    Posts
    78

    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!

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

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


  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  7. #7
    Join Date
    Jan 2008
    Posts
    78

    Re: Abstract class and vector issue

    Quote 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

    Quote 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...

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  9. #9
    Join Date
    Jan 2008
    Posts
    78

    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.

  10. #10
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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.


  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  12. #12
    Join Date
    Jan 2008
    Posts
    78

    Re: Abstract class and vector issue

    Then subclasses can decide either to define their own destructor or not.

    Thanks guys!

  13. #13
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    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.

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured