Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to create a poiter to an object of a class that contains overloaded operator?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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; <<==

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading