gpt4 book ai didi

C++ 错误 : because the following virtual functions are pure within

转载 作者:行者123 更新时间:2023-11-30 02:46:29 24 4
gpt4 key购买 nike

我在分配类中的对象时遇到问题。感觉我做对了,但它不让我跑。我已经查过了,但我无法弄清楚我做错了什么或与其他人不同。我已经注释掉了一堆东西,但它仍然无法运行

步枪.h

#ifndef rifle_H
#define rifle_H

#include <QPainter>
#include <QGraphicsItem>
#include <QGraphicsScene>

#include <QTimer>
#include <QDialog>

class rifle : public QGraphicsItem
{
public:
rifle();//(qreal dirx, qreal diry);
//virtual ~rifle();
void move();
protected:

private:
//QPixmap shot;
qreal m_Speed;
qreal m_DirX;
qreal m_DirY;
};

#endif // rifle_H

步枪.cpp

#include "rifle.h"

rifle::rifle() //qreal dirx, qreal diry
// : m_Speed(5.0)
// , m_DirX(dirx)
// , m_DirY(diry)
{
//shot.load(":/pic/rifle.png");
}

rifle::~rifle()
{

}


void rifle::move()
{
setPos(x()+m_Speed*m_DirX, y()+m_Speed*m_DirY);
}

这里是我写代码的地方

void Dialog::fire()
{
qreal dirx = ui->graphicsView->m_FireTarget.x()-200.0;
qreal diry = ui->graphicsView->m_FireTarget.y()-200.0;

qreal length = sqrt(dirx*dirx+diry*diry);
if (length!=0)
{
// normalized direction vector
qreal invLength= 1.0/length;
dirx *= invLength;
diry *= invLength;

// creating an angle perturbation of +/- 3°
qreal alphaPerturbation = static_cast<qreal>(qrand()%6-3) * M_PI / 180.0;
qreal xPerturbation = cos(alphaPerturbation);
qreal yPerturbation = sin(alphaPerturbation);
// cos(a+b)=...
dirx = dirx*xPerturbation - diry*yPerturbation;
// sin(a+b)=...
diry = diry*xPerturbation + dirx*yPerturbation;

/*rifle**/ circle = new rifle(dirx, diry);
scene->addItem(circle);
}
}

最佳答案

http://qt-project.org/doc/qt-4.8/qgraphicsitem.html

To write your own graphics item, you first create a subclass of QGraphicsItem, and then start by implementing its two pure virtual public functions: boundingRect(), which returns an estimate of the area painted by the item, and paint(), which implements the actual painting.

您必须在 rifle 中覆盖这些函数,因为它们是基类中的纯虚函数。

关于C++ 错误 : because the following virtual functions are pure within,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com