Vector iterator not dereferencable

前端 未结 3 893
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 09:27

I have an abstract base class called Shape from which both Circle and Rectangle are derived, but when I execute the following code in VS 2005 I get the error Debug assertion

相关标签:
3条回答
  • 2021-01-02 10:07

    This is a good reason to use boost::ptr_vector.

    It not only handles the fact that your objects need to be destroyed.
    xtofl@: You forgot the virtual destructor.

    But it also makes the members look like objects by returning references rather than pointers. This allows you to use the standard algorithms much more naturally rather than playing around with pointers in your 'equal' function (which is very un C++ like).

    #include <boost/ptr_container/ptr_vector.hpp>
    #include <iostream>
    
    class Shape
    {
        public:
            ~Shape()    {}
            bool operator==(Shape const& rhs) const
            {
                if (typeid(*this) != typeid(rhs))
                {
                    return false;
                }
    
                return this->isEqual(rhs);
            }
        private:
            virtual bool isEqual(Shape const& rhs) const    = 0;
    };
    
    class Circle: public Shape
    {
        public:
            Circle(int r)
                :radius(r)
            {}
        private:
            virtual bool isEqual(Shape const& r) const
            {
                Circle const&   rhs = dynamic_cast<Circle const&>(r);
                return radius == rhs.radius;
            }
            int radius;
    };
    class Rectangle: public Shape
    {
        public:
            Rectangle(int h,int w)
                :height(h)
                ,width(w)
            {}
        private:
            virtual bool isEqual(Shape const& r) const
            {
                Rectangle   const&  rhs = dynamic_cast<Rectangle const&>(r);
                 return (height == rhs.height) && (width == rhs.width);
            }
            int height;
            int width;
    };
    
    
    int main()
    {
    
        boost::ptr_vector<Shape>    data;
    
        data.push_back(new Circle(5));
        data.push_back(new Circle(6));
        data.push_back(new Rectangle(7,4));
    
        boost::ptr_vector<Shape>::iterator f;
        f = find(data.begin(),data.end(),Circle(6));
    
        std::cout << "Find(" << (f - data.begin() ) << ")" << std::endl;
    
    
    }
    
    0 讨论(0)
  • 2021-01-02 10:13

    Like @David Pierre suggests: find is value-based: it looks in the range of iterators for a pointer (e.g. 0x0F234420) that equals the pointer to the new Circle(point(1,2),3) you just created. Since that's a new object, it won't be there.

    You can get around this by using find_if with an operator that compares the objects referenced to by the pointer.

    However, the Criterium should be able to differentiate between shape types.

    class Shape {
    public:
        //amongst other functions
        virtual bool equal( const Shape* ) const = 0;
    };
    
    class Circle : public Shape {
    public:
        bool equal( const Shape* pOther ) const {
            const Circle* pOtherCircle = dynamic_cast<const Circle*>( pOther );
            if( pOtherCircle == NULL ) return false;
            // compare circle members
        }
    };
    
    class Rectangle : public Shape {
    public:
        bool equal( const Shape* pOther ) const {
            const Rectangle* pOtherR = dynamic_cast<const Rectangle*>( pOther );
            if( pOtherR == NULL ) return false;
            // compare rectangle members
        }
    };
    
    
    
    Shape* pFindThis = new Circle(point(1,2),3);
    vector<Shape*>::const_iterator itFound = find_if(s1.begin(),s1.end(), 
        bind1st( mem_fun( &Shape::equal ), pFindThis) ) );
    delete pFindThis; //leak resolved by Mark Ransom - tx!
    
    if( itFound != s1.end() ) {
        (*itFound)->move(point(10,20));
    }
    
    0 讨论(0)
  • 2021-01-02 10:22

    Simple :

    • find fails since your newly created Circle can't be found in the vector with comparing Shape *
    • a failed find returns the end iterator which is not deferencable as caught by a Debug assertion

    For it to work like you want, you do need to compare Shape, not Shape*

    As pointed out in other answers, boost::ptr_vector is an easy way to achieve this.

    0 讨论(0)
提交回复
热议问题