Reputation: 1
The error is : "member matrix::rows is not a type name" and "member matrix::init is not a type name". If it compiles successfully I shall have a matrix rows = ROWS and columns = COLS. What am i doing wrong here:
#include <vector>
class matrix
{
int ROWS{}, COLS{}, init{-1};
std::vector<std::vector<int>> table(ROWS, std::vector<int>(COLS, init));
};
Upvotes: 0
Views: 128
Reputation: 249173
You can write it like this:
class matrix
{
static constexpr int ROWS{}; // TODO: change to non-zero
static constexpr int COLS{}; // TODO: change to non-zero
static constexpr int init{-1};
std::vector<std::vector<int>> table{ROWS, std::vector<int>(COLS, init)};
};
Upvotes: 1
Reputation: 63
I don't understand the usage of the vector of vectors for this case. For such a case, I would propose to use a plain array.
template<int N, int M, typename D> struct Matrix {
public:
D get(int i, int j) {
return _data[index(i,j)];
}
void set(int i, int j, D v) {
_data[index(i,j)] = v;
}
private:
int index(int i, int j){
return i*N + j;
}
D _data[N*M];
};
So you don't really need std::vector. Just add some checks and you are good.
Upvotes: 0
Reputation: 11410
As John Zwinck points out, you need curly brackets. Why? Because, otherwise, the compiler takes
std::vector<std::vector<int>> table(ROWS, std::vector<int>(COLS, init));
as the definition of a member function, called table
, returning a std::vector<std::vector<int>>
and taking a first (unnamed) parameter of type ROWS
. At which point the compiler goes "Ugh, I don't know the type ROWS, let me throw a compilation error".
You can google Most Vexing Parse. https://en.wikipedia.org/wiki/Most_vexing_parse
Upvotes: 2