English 中文(简体)
如何调用静态函数?
原标题:How to call a static function?

我在类Matrix中定义了两个函数,如下所示(在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);

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,你可以这样调用它

A::foo();

阅读材料

您似乎正试图从该类的静态成员函数访问该类的变量。静态成员函数不能访问类的正则变量。

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

非静态与静态函数和变量





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签