0

I was trying to make an arc but getting the following error:

error: cannot allocate an object of abstract type 'arc'
                 arcItem = new arc(++id, startP, midP, endP);

Can you please help me out to solve the error. My code is as follows. I am getting error in cadgraphicsscene.cpp class at mousepress event.

cadgraphicsscene.cpp

void CadGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    {
        // mousePressEvent in the graphicsScene
        if(mouseEvent->button() == Qt::LeftButton)
        {
            switch (entityMode)
            {

            case ArcMode:
                if (mFirstClick)
                {
                    startP = mouseEvent->scenePos();
                    mFirstClick = false;
                    mSecondClick = true;
                }

                else if (!mFirstClick && mSecondClick)
                {
                    midP = mouseEvent->scenePos();
                    mFirstClick = false;
                    mSecondClick = false;
                    mThirdClick = true;
                }

                else if (!mSecondClick && mThirdClick)
                {
                    endP = mouseEvent->scenePos();
                    mThirdClick = false;
                    mPaintFlag = true;
                }

                if (mPaintFlag)
                {
                    arcItem = new arc(++id, startP, midP, endP);
                    itemList.append(arcItem);
                    mUndoStack->push(new CadCommandAdd(this, arcItem));
                    setFlags();
                }
            }
        }
   } 

arc.h

include <QGraphicsItem>

#include "qmath.h"
class arc : public QObject, public QGraphicsItem
{
    Q_OBJECT
public:
    arc(int, QPointF, QPointF, QPointF);
    arc(int, QLineF, QLineF);


    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                       QWidget *widget);
    enum { Type = UserType + 6 };
    int type() const;
    int id;

    QPointF startP, midP, endP, p1, p2, p3,center;
    QLineF lineBC;
    QLineF lineAC;
    QLineF lineBA;
    QLineF lineOA;
    QLineF lineOC;
    QLineF bisectorBC;
    QLineF bisectorBA;
    QGraphicsEllipseItem *ellipse;
    qreal rad;

private:
    QVector<QPointF> stuff;

};
4
  • 1
    you need to override boundingRect pure virtual member function as well, not only the paint one Nov 14, 2014 at 12:27
  • Sorry I didnt get you. Can you explain me with the help of example that how can I do that ? Nov 14, 2014 at 12:29
  • declare a member function in your arc class declaration: virtual QRectF boundingRect() const; and add proper implementation Nov 14, 2014 at 12:30
  • 1
    Thanks it helped me out too solve the error :) Nov 14, 2014 at 12:40

1 Answer 1

1

Its Obvious we cant instantiate Abstract class as in error message. Check out the definition of arc, it must have pure virtual function, derieve a class (with desired members) from it and make object of that class.

3
  • I have added the .h file of arc the arc has same defination as in the .h we can see. Can you please help me to solve the prob. Nov 14, 2014 at 12:31
  • Have you implemented arc.cpp?
    – user2233125
    Nov 14, 2014 at 12:33
  • 1
    arc doesnot seem to have pure virtual function, its possibly in public QObject or public QGraphicsItem. Find it and override it
    – Ali Kazmi
    Nov 14, 2014 at 12:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.