奇数cpp读取访问冲突

时间:2018-10-31 05:18:13

标签: c++

所以我一直在尝试调试此读取访问冲突大约两天,而我只是无法给出答案。

//Sorry there's so much, I didn't wanna leave out anything essential
#include <iostream>

struct Vec2
{
    int x, y;

    Vec2(){}
    Vec2(const int &a, const int &b)
    {
        x = a; y = b;
    }

    bool operator==(const Vec2 &other)
    {
        if (x == other.x && y == other.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};

enum Type 
{
    PAWN,
    KNIGHT,
    BISHOP,
    ROOK,
    QUEEN,
    KING
};

enum Side
{
    WHITE,
    BLACK
};

class Piece
{
private:
    Type type;
    Side side;
    Vec2 pos = Vec2(0, 0);
    bool inPlay = true;
public:
    Piece()
    {
        pos = Vec2(0, 0);
        type = PAWN;
        side = WHITE;
    }
    Piece(const Vec2 &p, const Type &t, const Side &s)
    {
        pos = p; type = t; side = s;
    }

    Vec2 getPos() //This is the function that's giving me the error
    {
        return pos;
    }

    /*void move(Piece &p, const Vec2 &v)
    {
        if (side == WHITE)
        {
            if (type == PAWN)
            {

            }
        }
        else
        {

        }
    }*/
};

class Board
{
private:
    Piece brd[32];
public:
    void init()
    {
        brd[0] = Piece(Vec2(0, 0), ROOK, WHITE);
        brd[1] = Piece(Vec2(1, 0), KNIGHT, WHITE);
        brd[2] = Piece(Vec2(2, 0), BISHOP, WHITE);
        brd[3] = Piece(Vec2(3, 0), QUEEN, WHITE);
        brd[4] = Piece(Vec2(4, 0), KING, WHITE);
        brd[5] = Piece(Vec2(5, 0), BISHOP, WHITE);
        brd[6] = Piece(Vec2(6, 0), KNIGHT, WHITE);
        brd[7] = Piece(Vec2(7, 0), ROOK, WHITE);
        for (int i = 8; i < 15; i++)
        {
            brd[i] = Piece(Vec2(i-8, 1), PAWN, WHITE);
        }

        brd[16] = Piece(Vec2(0, 7), ROOK, BLACK);
        brd[17] = Piece(Vec2(1, 7), KNIGHT, BLACK);
        brd[18] = Piece(Vec2(2, 7), BISHOP, BLACK);
        brd[19] = Piece(Vec2(3, 7), KING, BLACK);
        brd[20] = Piece(Vec2(4, 7), QUEEN, BLACK);
        brd[21] = Piece(Vec2(5, 7), BISHOP, BLACK);
        brd[22] = Piece(Vec2(6, 7), KNIGHT, BLACK);
        brd[23] = Piece(Vec2(7, 7), ROOK, BLACK);
        for (int i = 24; i < 31; i++)
        {
            brd[i] = Piece(Vec2(i-24, 6), PAWN, BLACK);
        }
    }

    int at(const Vec2 &v)
    {
        bool found = false;
        int i = 0;
            while (!found)
            {
                if (brd[i].getPos() == v)
                {
                    std::cout << "Found!" << std::endl;
                    return i;
                    break;
                }
                i++;
            }
            std::cout << "Piece at " << v.x << ", " << v.y << " Not found" << std::endl;
            return -1;
    }
};

int main()
{
    Board b;
    b.init();
    b.at(Vec2(2, 5));
    b.at(Vec2(3, 7));

    system("PAUSE");
    return 0;
}

我对错误不了解的是,Piece类的构造函数访问的内存部分与getPos()相同,并且在其之前被调用,但是调用时没有错误。

我尝试查看导致错误的调用堆栈和内存。内存未分配,这与错误相关,但由于构造函数为Piece.pos提供了一个值,因此没有意义。也许我只是愚蠢的人。

1 个答案:

答案 0 :(得分:3)

这行就在这里

b.at(Vec2(2, 5));

Piece的{​​{1}}中,在Board::brd数组中寻找pos。问题是,没有一个。这意味着该循环将永远运行:

Vec2(2, 5)

只要是 while (!found) { if (brd[i].getPos() == v) { std::cout << "Found!" << std::endl; return i; break; } i++; } ,循环就会继续。最终,此循环访问此应用程序不拥有的某些内存。在我的系统上,恰好是!found。然后,您会得到一个错误。因此,要解决此问题,必须在索引仍位于brd[173]数组内的情况下循环。一种快捷的解决方法是将条件更改为此:

brd