I have some questions. I created a class that contains operator overloading:
double MMatrix::operator()(int i, int j)const
{
return A[j + i * nCols];
}
To Pass an instance of that class to a function efficiently, I used to create a pointer of that class as the function formal parameter. And I want to use this overloaded operator in such a pointer to the class object within that function:
void Set_Matrix(MMatrix* M)
{
size_t m = M->NRows();
size_t n = M->NCols();
for (unsigned int i = 0; i < m; i++)
M(i, i) = 2; //This line gives error
}
The last line in that function gives an error message:
E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type
I would appreciate you if you give me the solution to this problem.
>Solution :
Cleaner syntax if you pass as a reference
void Set_Matrix(MMatrix& M)
{
size_t m = M.NRows();
size_t n = M.NCols();
for (unsigned int i = 0; i < m; i++)
M(i, i) = 2; <<==
}
other siwes you need
void Set_Matrix(MMatrix* M)
{
size_t m = M->NRows();
size_t n = M->NCols();
for (unsigned int i = 0; i < m; i++)
(*M)(i, i) = 2; <<==
}