重写枚举时的SIGSEGV

时间:2017-08-29 20:14:10

标签: c++ qt

我在赋值运算符中使用了SIGSEGV,正是在重写枚举时 我的结构:

struct Pawn
{
private:
    Q_GADGET
public:
    Q_PROPERTY(Position position MEMBER m_position)
    Q_PROPERTY(PawnType pawnType MEMBER m_pawnType)
    Pawn() :  m_position(Position::NotDefined), m_pawnType(PawnType::None) {}
    Pawn(const Pawn &a_other)
    {
        this->m_pawnType = a_other.m_pawnType;
        this->m_position = a_other.m_position;
    }
    Pawn &operator=(const Pawn &a_other)
    {
        this->m_pawnType = a_other.m_pawnType;
        this->m_position = a_other.m_position;
        return *this;
    }
    ~Pawn(){}

    enum Position
    {
        NotDefined = 0,
        Bottom,
        Left,
        Top,
        Right

    };

    enum PawnType
    {
        None = 0,
        Circle,
        Square,
        Diamond,
        Triangle,
    };

    Q_ENUMS(Position)
    Q_ENUMS(PawnType)

    Position m_position;
    PawnType m_pawnType;

};



typedef Pawn::PawnType PawnType;
typedef Pawn::Position Position;

Q_DECLARE_METATYPE(Pawn)
Q_DECLARE_METATYPE(Pawn::Position)
Q_DECLARE_METATYPE(Pawn::PawnType)

导致错误的行:

m_board[a_pawnPosition.y()][a_pawnPosition.x()] = m_board[current.y()][current.x()];
m_board[current.y()][current.x()] = Pawn();

m_board的定义:

Pawn m_board[10][10];

a_pawnPositioncurrent在界限

它并不总是发生。我想Q_PROPERTY可能会导致它,但我需要它们从QML中读取值并且不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

你是对的。 current超出范围,因为它是引用,而不是复制值,有时我会在QList前几行删除它。谢谢你的回答。