0

I have a problem using static_cast. Here is my program:

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

When I run it, it says: no match for call to '(Mtx) (int&, int&)' And I don't understand what's wrong and how should I modify it? It should be written using virtual functions, but I don't know how can I write it correctly. Can someone help me? This program should count the sum of all the elements of the FullMatrix and SymmetricMatrix.

Caboom
  • 11
  • 2

1 Answers1

0

remove virtual here

virtual double& entry(int i, int j){
    return (*this)() (i,j);    // error appears here
}

add virtual there

class Mtx {
...
    virtual double& operator() (int i, int j) = 0;
}

let the derived classes overload that operator and voala.

class FullMtx: public Mtx {
    ...
    virtual double& operator() (int i, int j) override { return mx[i] [j]; }
}

also this is just nonsense as already pointed out. you cast to same type, not the derived one. besides that you can't cast to derived type because in base you don't have the information about full inheritance - what if somebody derives from FullMtx without your knowledge ?

Mtx& ReferToDerived() {
    return static_cast<Mtx&>(*this);
}
rAndom69
  • 755
  • 4
  • 7
  • _"and voala"_ That's a new one – Lightness Races in Orbit Apr 29 '16 at 12:21
  • I added and removed those virtual as you said. But in class Mtx { ... virtual double& operator() (int i, int j) = 0; } I get an errors that say: " 'virtual' outside class declaration " and " 'double& operator()(int, int)' must be a nonstatic member function ". – Caboom Apr 29 '16 at 12:42