如何调用静态函数?

发布于 2024-11-10 09:20:35 字数 1849 浏览 2 评论 0原文

我在我的类 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 不是问题。 请提出您的意见来解决这个问题。谢谢!

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老旧海报 2024-11-17 09:20:35

如果您有一个名为 A 的类,并且它有一个静态函数 foo,您可以这样调用它

A::foo();

阅读材料

If you have a class called A and it has a static function foo you would call it this way

A::foo();

Reading material

总攻大人 2024-11-17 09:20:35

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

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

非静态与静态函数和变量

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文