I am currently working on a simple Scrabble implementation for a college project.
I can’t get a part of it to work, though!
Check this out:
My board.h:
The subroutine where the error lies:
//Following snippet contained in board.cpp
//I believe the function is self-explanatory...
//Pos is a struct containing a char, y, a int, x and an orientation, o, which is not //used in this particular case
void Board::showBoard()
{
Pos temp;
temp.o = 0;
for (temp.y = 'A'; temp.y < (65 + TOTAL_COLUMNS); ++temp.y)
{
for (temp.x = 1; temp-x < (1 + TOTAL_ROWS); ++temp.x)
{
cout << _matrix[temp].getContents();
}
cout << endl;
}
}
The errors returned on compile time:
How come the error states that I am trying to compare two Pos when I am comparing chars and ints?
I also really can’t place these other errors…
Thanks for your time!
EDIT:
Since my whole project depends on Pos, I am going to try overloading the < operator for it… Can anyone give me a few tips on that? Keep in mind, I’m a beginner!
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
constvariables instead of#defines. In this case, you could put the following in yourBoard.cpp:You can then make them available through your header by putting these in
Board.h:Even cleaner is to declare them as class members. Put these in
Board.cpp:And in
Board.hpp, inside thepublicsection of theclassdefinition:They have to be
staticbecause they do not belong to any specificBoardinstance, but are rather properties of the class as a whole. You can then access them from outside theBoardclass by writingBoard::TOTAL_ROWSetc.The other problem here is that you are creating a
map<Pos, Cell>. Themaptemplate requires that its key type (Pos) has a valid<operator defined on it; internally, themapsorts 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
Posobjects. I would not recommend that to a beginner, becausemapstars misbehaving, and>,<=, and>=,==and!=.That being said, here is the code. This assumes that two
Posobjects with the samexandyvalues are considered equal; it does not look at the value ofo(which is a weird thing to have in a “coordinate” type anyway, and I don’t know what it’s used for).The other (better) option is to abandon the
Postype completely, and represent your board as a two-dimensional array, or avectorofvectors. Its type would then bevector<vector<Cell> >. (Note the space between> >! Without it, this will be parsed as the right-shift operator>>!)