#define TOTAL_ROWS 15;
#define TOTAL_COLUMNS 15;
Theses are preprocessor definitions, which must not end in a semicolon. The semicolon will become part of the substitution text, so the compiler sees something like (65 + 15;)
which is clearly wrong.
In C++, it is better to use const
variables instead of #define
s. In this case, you could put the following in your Board.cpp
:
const unsigned int TOTAL_ROWS = 15;
const unsigned int TOTAL_COLUMNS = 15;
You can then make them available through your header by putting these in Board.h
:
extern const unsigned int TOTAL_ROWS;
extern const unsigned int TOTAL_COLUMNS;
Even cleaner is to declare them as class members. Put these in Board.cpp
:
const unsigned int Board::TOTAL_ROWS = 15;
const unsigned int Board::TOTAL_COLUMNS = 15;
And in Board.hpp
, inside the public
section of the class
definition:
static const unsigned int TOTAL_ROWS;
static const unsigned int TOTAL_COLUMNS;
They have to be static
because they do not belong to any specific Board
instance, but are rather properties of the class as a whole. You can then access them from outside the Board
class by writing Board::TOTAL_ROWS
etc.
The other problem here is that you are creating a map<Pos, Cell>
. The map
template requires that its key type (Pos
) has a valid <
operator defined on it; internally, the map
sorts its elements using this operator, so it can do fast lookups. The error occurs only at the point where you try to look something up in the map; this is due to the way templates work, so don't break your head over it right now.
One solution is to overload this operator yourself to define an ordering on Pos
objects. I would not recommend that to a beginner, because
- operator overloading is an advanced technique, and
- you have to be very careful to define consistent behaviour, or else the
map
stars misbehaving, and
- if you do this, you should also overload
>
, <=
, and >=
, ==
and !=
.
That being said, here is the code. This assumes that two Pos
objects with the same x
and y
values are considered equal; it does not look at the value of o
(which is a weird thing to have in a "coordinate" type anyway, and I don't know what it's used for).
bool operator<(Pos const &l, Pos const &r) {
if (l.y < r.y) return true;
if (l.y > r.y) return false;
if (l.x < r.x) return true;
if (l.x > r.x) return false;
return false;
}
The other (better) option is to abandon the Pos
type completely, and represent your board as a two-dimensional array, or a vector
of vector
s. Its type would then be vector<vector<Cell> >
. (Note the space between > >
! Without it, this will be parsed as the right-shift operator >>
!)