开发者

C++ Beginner - Trouble using structs and constants!

开发者 https://www.devze.com 2022-12-30 02:54 出处:网络
I am currently working on a simple Scrabble implementation for a college project. I can\'t get a part of it to work, though!

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:

http://pastebin.com/J9t8VvvB

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;开发者_如何学Go

    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:

http://pastebin.com/bZv7fggq

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!


#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 #defines. 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 vectors. Its type would then be vector<vector<Cell> >. (Note the space between > >! Without it, this will be parsed as the right-shift operator >>!)

0

精彩评论

暂无评论...
验证码 换一张
取 消