I have written a small code which isolates my flawed approach/understanding in using macros to access or assign values to elements in a std::vector. Below is the snippet of the code.
#define mat(i,j,nrows) mat[((j)*(nrows))+(i)]
struct _STR1
{
int nRows, nCols;
std::vector < double >mat;
std::vector < double >anothermat;
};
void Create_Data (int &nC, _STR1 * &_str)
{
_str = new _STR1[nC];
for (int myid = 0; myid < nC; myid++)
{
_str[myid].nRows = 100;
_str[myid].nCols = 3;
_str[myid].mat.resize (_str[myid].nRows * _str[myid].nCols);
_str[myid].anothermat.resize (_str[myid].nRows * _str[myid].nCols);
for (int i_row = 0; i_row < _str[myid].nRows; i_row++)
{
_str[myid].mat (i_row, 0, _str[myid].nRows) = 1.0e0;
_str[myid].mat (i_row, 1, _str[myid].nRows) = 1.0e0;
_str[myid].mat (i_row, 2, _str[myid].nRows) = 1.0e0;
_str[myid].anothermat (i_row, 2, _str[myid].nRows) = 1.0e0;
}
}
}
If I comment "_str[myid].anothermat (i_row, 2, _str[myid].nRows) = 1.0e0;", I do not get any error. Otherwise I get the following error
error: call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
I think my understanding in using a macro is wrong but I am unable to understand why this is so.
Can anyone please tell me why this approach is wrong and why I have the error in one case whereas the other does not. Is my usage of macro correct?