Thread: Destructor inaccessible

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    18

    Destructor inaccessible

    Hi,

    My program used to compile perfectly, until I've added a member to a class that consists of an object of another class. Now, I get the following warning:

    "Class 'Reta': destructor could not be generated because base class destructor is inaccessible."

    and the following error:

    "'osg::LineSegment::~osg::LineSegment': cannot access protected member declared in class 'osg::LineSegment'"

    I've looked in the documentation and my osg::LineSegment class has a protected virtual destructor

    Code:
          protected:
                    virtual ~LineSegment();
    So basically, I have two situations in which I cannot access my destructor...
    I'm going to post the source code (my class definition) just to add some information to my question, hoping that the fact that it's written in Portuguese won't be much of an obstacle to understanding it.

    Here it goes:

    Code:
    #ifndef RETA_H
    #define RETA_H
    
    #include <osg/LineSegment>
    
    #include "primitiva.h"
    #include "plano.h"
    
    class Plano;
    
    // Classe 'Reta' derived from 'Primitiva'
    class Reta : public Primitiva
    {
    public:
    	Reta();
    	osg::Geode* criarReta(osg::Vec3f posicao, float raio, float altura);
    	void RetaParalela(Reta* reta, osg::Vec3f pos);
    	void RetaParalela(Plano* plano, osg::Vec3f pos);
    	void RetaPerpendicular(Reta* reta, osg::Vec3f pos);
    	void RetaPerpendicular(Plano* plano, osg::Vec3f pos);
    private:
    	osg::LineSegment line; // this is the member that I added
    };
    
    #endif

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've looked in the documentation and my osg::LineSegment class has a protected virtual destructor
    This means that your own destructor cannot destroy the osg::LineSegment member variable because its destructor cannot be called.

    Two options come to mind:
    1. Subclass osg::LineSegment, declaring the destructor with public access, and then use a member variable of this subclass in your class.

    2. Use a (smart) pointer or reference to osg::LineSegment as a member variable instead. In this case it is expected that objects of subclasses of osg::LineSegment would be instantiated (i.e., osg::LineSegment is used as a polymorphic base class).

    "Class 'Reta': destructor could not be generated because base class destructor is inaccessible."
    Strangely though, this seems to say that Primitiva's destructor is private. But the compiler should have reported the problem even before you added the osg::LineSegment member variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want the class to be used, you probably don't want a private/protected destructor - you want anyone who can create an object to be able to destroy it, which means public destructors.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    you probably don't want a private/protected destructor
    That's what I've thought too. But that class wasn't mine. It was already in a package for OpenSceneGraph developers. There must be some reason for that...

    Anyway, I've simply changed my member from an object to a pointer and my program compiled successfully. Thanks.

    Renan M Z Mendes

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's what I've thought too. But that class wasn't mine. It was already in a package for OpenSceneGraph developers. There must be some reason for that...
    You might want to check the docs and see how LineSegment really is intended to be used.

    Anyway, I've simply changed my member from an object to a pointer and my program compiled successfully. Thanks.
    You're welcome, but remember that if you are using a raw pointer you have to do memory management yourself, e.g., implement the copy constructor, copy assignment operator, and destructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    A key to design, at least according to the GoF, that using a class as a member is generally better than trying to inherit from it (not always true but this seems to be one such case) if you don't need to pass the derived object as the base object ever, then inheritance is overkill, and just helps to create too much class coupling.

Popular pages Recent additions subscribe to a feed