Ich habe ein Problem mit static_cast. Hier ist mein Programm:C++ gibt mir einen Fehler: keine Übereinstimmung für den Aufruf an
#include <iostream>
using namespace std;
class Mtx { // base matrix
private:
// refer to derived class
Mtx& ReferToDerived() {
return static_cast<Mtx&>(*this);
}
// entry() uses features of derived class
virtual double& entry(int i, int j){
return ReferToDerived() (i,j); // error appears here
}
protected:
int dimn; // dimension of matrix
public:
// define common functionality in base class that can
// be called on derived classes
double sum() { // sum all entries
double d = 0;
for (int i = 0; i < dimn; i++)
for (int j = 0; j < dimn; j++) d += entry(i,j);
return d;
}
};
class FullMtx: public Mtx {
double** mx;
public :
FullMtx(int n) {
dimn = n;
mx = new double* [dimn] ;
for (int i=0; i<dimn; i++) mx[i] = new double [dimn];
for (int i=0; i<dimn; i++)
for (int j=0; j<dimn; j++)
mx[i][j] = 0; // initialization
}
double& operator() (int i, int j) { return mx[i] [j]; }
};
class SymmetricMtx : public Mtx {
// store only lower triangular part to save memory
double** mx ;
public :
SymmetricMtx(int n) {
dimn = n;
mx = new double* [dimn];
for (int i=0; i<dimn; i++) mx[i] = new double [i+1];
for (int i=0; i<dimn; i++)
for (int j=0; j <= i; j++)
mx[i][j] = 0; // initialization
}
double& operator() (int i, int j) {
if (i >= j) return mx[i][j] ;
else return mx[j][i]; // due to symmetry
}
};
int main()
{
FullMtx A(1000);
for (int i=0; i<1000; i++)
for (int j=0; j<1000; j++)
A(i,j)=1;
cout << "sum of full matrix A = " << A.sum() << '\n';
SymmetricMtx S(1000); // just assign lower triangular part
for (int i=0; i<1000; i++)
for (int j=0; j<1000; j++)
S(i,j)=1;
cout << "sum of symmetric matrix S = " << S.sum() << '\n';
}
Wenn ich es laufen, heißt es: kein Spiel für Aufruf ‚(MTX) (int &, int &)‘ Und ich verstehe nicht, was falsch ist und wie sollte ich ändern Sie es? Es sollte mit virtuellen Funktionen geschrieben werden, aber ich weiß nicht, wie ich es richtig schreiben kann. Kann mir jemand helfen? Dieses Programm sollte die Summe aller Elemente der FullMatrix und SymmetricMatrix zählen.
'zurück static_cast (* dies);' macht überhaupt keinen Sinn. –
warum nicht? wie soll ich das ändern? – Caboom
@Paula Es ist äquivalent zu 'return * this;'. Es ist nicht klar, was Sie erwarten. – molbdnilo