So I'm defining a class sqmatrix of square matrices for fun and learning, and I've successfully defined a function submat that outputs a submatrix of the object constructed in a certain way:
sqmatrix sqmatrix::submat (unsigned int row, unsigned int col)
{ /* code */ return smat; }
Now I want to define another function that takes the submatrix constructed by submat and outputs, say, the matrix where all elements have been multiplied by 42. To do so, I wrote
sqmatrix sqmatrix::cofact (unsigned int srow, unsigned int scol)
{
sqmatrix cfac = 42 * m_mat.submat(srow, scol);
return cfac;
}
where I'd previously overloaded * to work with my objects, and m_mat has been declared in the header of the class as a vector of vectors containing long long ints. However, this did not compile, so I went for member function pointers and wrote:
sqmatrix sqmatrix::cofact (unsigned int srow, unsigned int scol)
{
sqmatrix (sqmatrix::*point)(unsigned int, unsigned int);
point = &sqmatrix::submat;
sqmatrix cfac = 42 * (m_mat.*point)(srow, scol);
return cfac;
}
However, this does not compile either. Here are the relevant lines in the header file:
private:
// ...
std::vector< std::vector<long long int> > m_mat;
public:
// ...
sqmatrix submat(unsigned int row, unsigned int col);
sqmatrix cofact(unsigned int srow, unsigned int scol);
Compiler says:
Error: pointer to member type
sqmatrix (sqmatrix::)(unsigned int,unsigned int)incompatible with object typestd::vector< std::vector<long long int> >
What am I getting wrong?
Mmm. I think you're wanting:
No idea what kind of matrix operation you're actually trying to do, but if you're trying to take the submatrix of
thisand then multiply it by a constant 42, then you just need to callsubmat(srow, scol).The way you've written it, you're trying to call a member function of a vector, instead of a member function of your class which contains the vector.
C++ allows you to also call
this->submat(srow, scol)which may make it more clear what you're actually doing, but most of the time you see people call member functions with no reference tothis, as its perfectly valid C++ and also shorter.