0

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!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
aki
  • 1
  • 1
  • 3
  • Can you provide the code for how you're calling the method? I think you're close... – Corey Ogburn May 31 '11 at 13:54
  • 1
    possible duplicate of [non-static vs. static function and variable.](http://stackoverflow.com/questions/2285915/non-static-vs-static-function-and-variable) – Woot4Moo May 31 '11 at 13:54
  • 4
    The error does not appear to happen in the code you show us (the message says it is in `Matrix::MatDet`. Please post that code, and indicate the line where the error is. – Björn Pollex May 31 '11 at 13:56
  • The problem seems to be inside **MatDet**, which is not on your code. – karlphillip May 31 '11 at 13:56
  • Since there was at least one typo in the error message (now fixed), I wonder if the square brackets are also typos? It is unhelpful to omit the line number and other information too. – Jonathan Leffler May 31 '11 at 14:00
  • As others have said, you haven't show the code that's actually the problem. But I suspect you need to learn about [const correctness](http://stackoverflow.com/questions/136880/sell-me-on-using-const-correctness), whether or not that's your problem here. – Fred Larson May 31 '11 at 14:02
  • I'll second Fred: when the error message shows that there is no call for `T::T(T)` (regardless of the type `T`), and lists as a candidate `T::T(T&)`, it's a good bet that a `const` is missing in the declaration of the copy constructor. – James Kanze May 31 '11 at 14:16
  • @Corey, I want to use my MatCopy fuction inside Matinverse function so i used 'Matrix aa = Matrix::MatCopy(x);' to call it – aki May 31 '11 at 14:22
  • May I ask what's the whole point about this static `MatCopy` function anyway? I first copies all the entries from `a`, one by one, to the _temporary_ Matrix `P`. And then it returns `P`, **by value**, which means the copy constructor needs to be called! – leftaroundabout May 31 '11 at 17:15

2 Answers2

4

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

A::foo();

Reading material

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
0

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

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426