Yet another Scrabble project question… This is a simple one.
It seems I am having trouble getting my global constants recognized:
My board.h:
http://pastebin.com/7a5Uyvb8
Errors returned:
1>C:\Users\Francisco\Documents\FEUP\1A2S\PROG\projecto3\projecto3\Board.h(34): error: variable "TOTAL_ROWS" is not a type name
1> vector< vector<Cell> > _matrix(TOTAL_ROWS , vector<Cell>(TOTAL_COLUMNS));
1>
1>main.cpp
1>compilation aborted for .\Game.cpp (code 2)
1>Board.cpp
1>.\Board.h(34): error: variable "TOTAL_ROWS" is not a type name
1> vector< vector<Cell> > _matrix(TOTAL_ROWS , vector<Cell>(TOTAL_COLUMNS));
1> ^
1>
Why does this happen? Why is the compiler expecting types?
Thanks for your time!
EDIT:
Disregard my previous edit…
This is my default constructor:
Board::Board()
{
_matrix(TOTAL_ROWS, vector(TOTAL_COLUMNS));
}
I get the following error.
1>.\Board.cpp(16): error: call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
1> _matrix(TOTAL_ROWS, vector<Cell>(TOTAL_COLUMNS));
1> ^
Why does this happen?
I managed to solve all the problems with my file. I used
Board::Board() :
_matrix(TOTAL_ROWS, vector<Cell>(TOTAL_COLUMNS))
{}
instead. Thanks for all your help!
The way that is written, you are defining a function called _matrix that returns a vector. So
TOTAL_ROWSis expected to be a type name, since it is being parsed as a paramter type. I assume what you are trying to do is define a variable called _matrix that is a vector.What you want to do is leave off the constructor, and initialize the variable inside your constructor. Only constant integral values can be initialized in the body of the class, at least in the current version of the standard.
Leaving off the unimportant parts:
Note that this is just an example. Presumably you have an implementation file with an actual body for
Board(), and you should put the initialization there rather than directly in the header or you’ll get errors. The important thing is that you should not do it when you declare_matrixinitially.For your new question,
extern const unsigned int TOTAL_COLUMNS = 15;definesTOTAL_COLUMNSevery time Board.h is included by a file. Constant variables at namespace scope have internal linkage by default, so if you leave off theexternyou will be okay.In general, if the variable isn’t constant, you take an approach similar to the one for
_matrix. You leave off the initialization in the header, and then inside an implemenation file put it back on:board.h:
extern const int TOTAL_COLUMNS;board.cpp:
extern const int TOTAL_COLUMNS = 15;