如何调用静态函数?

时间:2011-05-31 13:52:32

标签: c++ static-methods

我已经在我的班级Matrix中定义了2个函数,如下所示(在Matrix.hpp中)

static Matrix MCopy( Matrix &a );   
static Matrix MatInvert( Matrix &x )
static double MatDet( Matrix &x ); // Matdef function

在我的Matrix.Cpp文件中,我按如下方式定义了这些函数:

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

好的,这是我的MatDef功能

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

但是当我编译它时,我收到一个错误告诉我:

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&]’:

由于我不熟悉C ++编程,所以无法理解问题是什么,所以请帮我解决这个错误。

我用过的地方

  Matrix aa = Matrix::MatCopy(x);

它显示了与第306行相同的错误消息,但有不同的注释,所以我认为MatDef不是问题。 请给出你的意见来解决这个问题。谢谢!

2 个答案:

答案 0 :(得分:4)

如果你有一个名为A的类,并且它有一个静态函数foo,你会这样称呼它

A::foo();

Reading material

答案 1 :(得分:0)

您似乎正在尝试从该类的静态成员函数 访问类的变量。静态成员函数无法访问类的常规变量。

您需要查看@ Woot4Moo建议的链接:

non-static vs. static function and variable