如何调用静态函数? [英] How to call a static function?
问题描述
我在我的类 Matrix
中定义了 2 个函数,如下(在 Matrix.hpp 中)
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
在我的 Matrix.Cpp 文件中,我将这些函数定义如下:
In my Matrix.Cpp file, 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;
}
好的,这是我的 MatDef 函数
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&]’:
我不明白问题出在哪里,因为我是 C++ 编程的新手,所以请帮助我修复这个错误.
I can not understand what the problem is since I am new to C++ programming, so please help me in fixing this error.
我用过的地方
Matrix aa = Matrix::MatCopy(x);
它显示与第 306 行相同的错误消息,但有不同的注释,所以我认为 MatDef
不是问题.请提出您的意见以解决此问题.谢谢!
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!
推荐答案
如果你有一个叫做 A 的类并且它有一个静态函数 foo 你可以这样调用它
If you have a class called A and it has a static function foo you would call it this way
A::foo();
这篇关于如何调用静态函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!