gpt4 book ai didi

c++ - QT变量赋值,<没有这样的值>,SIGSEGV

转载 作者:行者123 更新时间:2023-11-28 04:42:41 25 4
gpt4 key购买 nike

我有 3 个类,每个类都有 3 个 int 类型的变量。还有 3 个成员函数,我在其中尝试将这些变量分配给动态数组并返回数组。在第一种情况下一切都很好,当我尝试从第二类和第三类分配变量时出现问题。

#ifndef TANK_H
#define TANK_H

#include <QObject>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>

class Tank : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
explicit Tank(QObject *parent = nullptr);
void setTanks(int x, int y, QGraphicsScene * scene);
void addTanks(int howMany = 0);
int * showStatisticsOfTank();

protected:
QGraphicsScene * scene;
int coordinateX, coordinateY;

int amountOfTanks;
int attack;
int defence;
int cost;
signals:
public slots:
};

#endif // TANK_H

在 .cpp 文件中:

int *Tank::showStatisticsOfTank()
{
int * arr = new int[3];
arr[0] = amountOfTanks;
arr[1] = attack;
arr[2] = defence;
return arr;
}

这有效...但是:

#ifndef WARPLANE_H
#define WARPLANE_H

#include <QObject>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>

class Warplane : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT
public:
explicit Warplane(QObject *parent = nullptr);
void setWarplane(int x, int y, QGraphicsScene * scene);
void addWarplane(int howMany = 0);
int * showStatisticOfWarplane();

protected:
QGraphicsScene * scene;
int coordinateX, coordinateY;

int amountOfWarplanes;
int attack;
int defence;
int cost;
signals:
public slots:
};

#endif // WARPLANE_H

在 .cpp 文件中:

int *Warplane::showStatisticOfWarplane()
{
int * arr = new int[3];
arr[0] = amountOfWarplanes; //HERE ERROR SHOWS UP
arr[1] = attack;
arr[3] = defence;
return arr;
}

这不是...出现 SIGSEGV 错误并且调试器在该行停止。当我试图查看 amountOfWarplanes,attack and defense 中的值时,它说:

<no such value>. 

第三类是完全一样的,当我注释掉第二类错误时,第三类错误出现在同一个地方。我尝试清理所有 -> 重建,但没有用。

最佳答案

SIGSEGV 在尝试访问 amountOfWarplanes 时发生,它是 Warplane 类的内部成员。这意味着它发生在尝试访问 this 时。

这些情况表明 Warplane 指针无效。

关于c++ - QT变量赋值,<没有这样的值>,SIGSEGV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49899236/

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