How to call a static function?
I have defined 2 functions in my class Matrix
as follows (in Matrix.hpp)
static Matrix MCopy( Matrix &a );
static Matrix MatInvert( Matrix &x )
static double MatDet( Matrix &x ); // Matdef function
In my Matrix.Cpp fil开发者_运维技巧e, I defined these functions as follows:
Matrix Matrix::MatCopy( Matrix &a )
{
Matrix P( a.getRow() , a.getCol() , Empty );
int j=0;
while( j != P.getRow() ){
int i=0;
while( i != P.getCol() ){
P(j,i)=a(j,i);
++i;
}
++j;
}
return P;
}
Matrix Matrix::MatInvert( Matrix &x )
{
Matrix aa = Matrix::MatCopy(x); // i got error message here
int n = aa.getCol();
Matrix ab(n,1,Empty);
Matrix ac(n,n,Empty);
Matrix ad(n,1,Empty);
if(MatLu(aa,ad)==-1){
assert( "singular Matrix" );
exit(1);
}
int i=0;
while( i != n ){
ab.fill(Zero);
ab (i,0)=1.0;
MatRuecksub(aa, ab,ac,ad,i);
++i;
}
return ac;
}
ok this is the my MatDef function
double Matrix::MatDet( Matrix &x )
{
double result;
double vorz[2] = {1.0, -1.0};
int n = x.getRow();
Matrix a = Matrix::MatCopy(x);
Matrix p( n, 1, Empty);
int i = MatLu(a, p);
if(i==-1){
result = 0.0;
}
else {
result = 1.0;
int j=0;
while(j != n){
result *= a( static_cast<int>(p(j,0)) ,j);
++j;
}
result *= vorz[i%2];
}
return result;
}
but when I compile this, I get an error telling me that:
line 306:no matching function for call to ‘Matrix::Matrix[Matrix]’:
note: candidates are: Matrix::Matrix[Matrix&]
note:in static member function ‘static double Matrix ::MatDet[Matrix&]’:
I can not understand what the problem is since I am new to C++ programming, so please help me in fixing this error.
Where I have used
Matrix aa = Matrix::MatCopy(x);
It shows same error message like line 306 but with different notes so I think MatDef
is not a problem.
Please give your comments to solve this. Thanks!
If you have a class called A and it has a static function foo you would call it this way
A::foo();
Reading material
It seems that you are trying to access a variable of the class from a static member function of that class. Static member functions cannot access regular variables of the class.
You need to check the link that @Woot4Moo suggested:
non-static vs. static function and variable
精彩评论