I have an abstract class (pure virtual function in it) and I use polymorphism so it'd contain an address of a class inheriting of that class. The 'parent' is called Piece
and the class containing a pointer to it is Square
. Anyways, I'm getting a weird exception, with the following code:
Header files:
#pragma once //Square.h
class Piece; //as I cannot include piece.h, if i do, I actually include myself, because piece.h includes board.h and board.h includes me.
class Square
{
public:
Square(void);
Piece* getPiece();
void setPiece(Piece* piece);
~Square(void);
private:
Piece* _piece;
};
And a second one:
#pragma once
#include "Square.h"
class Board
{
public:
Board(void);
Square& getSquare(int row, int col);
~Board(void);
private:
Square** _square;
};
And a third for last:
#pragma once
#include <iostream>
#include "Coord.h"
#include "Board.h"
using namespace std;
class Piece //irellevant variables though, for this question
{
public:
Piece(Coord pos, bool color);
virtual bool isLegalMove(Coord c, Board* b) const = 0;
virtual char getPieceChar() const = 0;
~Piece(void);
protected:
Coord _pos;
bool _color;
};
and the code itself wherever I crash upon is Square's getPiece()
function (when I call it from the main):
Piece* Square::getPiece()
{
return this->_piece;
}
I know that in that square, the _piece
Piece pointer is null, and yet the program crashes here.
I'm on windows with VS 2012 Ultimate and here is the Exception message:
Access violation reading location 0x000000C4.
and it says about this->_piece
(when I look at the bottom variables), 'unable to read memory', but that's to expect as the pointer is NULL.
Any idea what's going on, making my program crash ?
User contributions licensed under CC BY-SA 3.0