QT变量赋值,<no such =“”value =“”>,SIGSEGV

时间:2018-04-18 12:01:52

标签: c++ qt

我有3个clases,每个都有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中的值,攻击和防御它说:

<no such value>. 

第三个类是相同的,当我注释掉第二个类错误时,在同一个地方的第三个类中出现。我试着清理所有 - &gt;重建,不起作用。

1 个答案:

答案 0 :(得分:1)

The SIGSEGV happens when trying to access amountOfWarplanes which is an internal member of the class Warplane. This means it happens while trying to access this.

These conditions suggest that the Warplane pointer is invalid.