I have the following code:
template <typename M>
struct Matrix
{
Matrix(int size);//(int x = defaultRows, int y = defaultCols);
~Matrix();
int rowSize;
int colSize;
char alpha[4];
int getSize() const;
void displayAll();
M getElement(int x, int y);
M** aMatrix;
};
template <typename M>
Matrix<M>::Matrix(int size)
{
alpha = {'a', 'b', 'c', 'd'};
rowSize = size;
colSize = size + 1;
aMatrix = new M*[rowSize];
srand(time(NULL)); //set the random seed
for(int r = 0; r < size; r++)
{
aMatrix[r] = new M[colSize];
for(int c = 0; c < colSize; c++)
{
int randomNum = rand() % 19 + (-9);
aMatrix[r][c] = randomNum;
}
}
}
I don’t know why, but i keep getting errors when I try to complie the program. Its to do with my “alpha” char array (when I comment it out, it works fine).
I don’t see whats wrong with it at all…
The errors are:
Error 3 error C2143: syntax error : missing ‘;’ before ‘}’
Error 2 error C2143: syntax error : missing ‘;’ before ‘{‘
Error 1 error C2059: syntax error : ‘{‘
Don’t think I have missed anything out have I??
You can’t do this in C++ (well not for some time anyway):
It looks like an initializer but it’s not. Try:
EDIT
Not that it will help you much but C99 (along with some C++ compilers, as an extension) supports this: