Expected { at the end of output

So, as much as it is fast and convenient to put code into a header, it almost always leads to compile errors that take longer to track down than the time it takes to create the standard .h and .cpp files, especially with this much code.

I’m not exactly sure where the problem was, but it may have been the Matrix::operator>>(). You’ll be able to find errors like this by using an IDE like eclipse and pressing Ctrl-i to auto-indent the code. Also, please check that I didn’t mess up any of your logic formatting it.

Also, there’s some academic trick question about memory allocation for matrices. if I remember correctly, you are supposed to allocate all the memory at once and then use pointers to refer to the array elements. I could be wrong and I’m not going to go into it now. Something like:

int rows(10000);
int cols(10000);

double* _data = (double*) malloc(rows * cols * sizeof(double));
double** data = (double**) malloc(rows * sizeof(double*));

for(int i = 0; i < rows; i++) {
    data[i] = _data + (cols * i);
}
  • Please note that I have no idea if the matrix math is correct

matrix.h

#ifndef MATRIX_H
#define MATRIX_H

#include <istream>
#include <ostream>

class Matrix {
public:
    Matrix(std::size_t rows, std::size_t cols, double initValue);
    Matrix(const Matrix& m1);
    ~Matrix();

    Matrix& operator=(const Matrix& m1);
    double& operator()(std::size_t i, std::size_t j);
    const double& operator()(std::size_t i, std::size_t j) const;
    bool operator==(const Matrix& m1) const;
    bool operator!=(const Matrix& m1) const;
    Matrix& operator+=(const Matrix& m1);
    Matrix operator+(const Matrix& m1) const;
    Matrix& operator-=(const Matrix& m1);
    Matrix operator-(const Matrix& m1) const;
    Matrix operator*(const Matrix& m1) const;
    Matrix& operator*=(const Matrix& m1);
    std::size_t rows() const;
    std::size_t cols() const;

    friend std::ostream& operator<<(std::ostream& x, const Matrix& m1);
    friend std::istream& operator>>(std::istream& y, Matrix& m1);

private:
    int r, c, z;
    double *p;
};

#endif

matrix.cpp

#include "matrix.h"

#include <iostream>
#include <istream>
#include <ostream>

Matrix::Matrix(std::size_t rows, std::size_t cols, double initValue) :
        r(rows),
        c(cols),
        z(r * c) {
    p = new double [z];

    for(int i = 0; i < z; ++i) {
        p[i] = initValue;
    }
}

Matrix::Matrix(const Matrix& m1) :
        r(m1.r),
        c(m1.c),
        z(r*c) {
    p = new double [z];

    for (int i=0; i<z; ++i) {
        p[i]=m1.p[i];
    }
}

Matrix::~Matrix() {
    delete [] p;
}

Matrix& Matrix::operator=(const Matrix& m1) {
    if (*this == m1) {
        return *this;
    } else {
        r = m1.r;
        c = m1.c;
        z = r * c;

        delete[] p;

        p = nullptr;
        p = new double[m1.z];

        for(int i = 0; i < z; ++i) {
            p[i] = m1.p[i];
        }

        return *this;
    }
}

double& Matrix::operator()(std::size_t i, std::size_t j) {
    return p[i*c+j];
}

const double& Matrix::operator()(std::size_t i, std::size_t j) const {
    return p[i*c+j];
}

bool Matrix::operator==(const Matrix& m1) const {
    if(r==m1.r && c==m1.c) {
        for(int i=0;i<z;++i) {
            if (p[i] != m1.p[i]) {
                return false;
            }
        }
    } else if(r != m1.r || c != m1.c) {
        return false;
    }

    return true;
}

bool Matrix::operator!=(const Matrix& m1) const {
    if( r != m1.r || c != m1.c) {
        return true;
    }

    for(int i = 0; i < m1.z; ++i) {
        if (p[i] != m1.p[i]) {
            return true;
        }
    }

    return false;
}

Matrix& Matrix::operator+=(const Matrix& m1) {
    for(int i=0;i<z;++i) {
        p[i] = p[i] + m1.p[i];
    }

    return *this;
}

Matrix Matrix::operator+(const Matrix& m1) const {
    Matrix m3 (r,c,0);

    for(int i=0;i<z;++i) {
        m3.p[i] = p[i] + m1.p[i];
    }

    return m3;
}

Matrix& Matrix::operator-=(const Matrix& m1) {
    for(int i=0;i<z;++i) {
        p[i] = p[i] - m1.p[i];
    }

    return *this;
}

Matrix Matrix::operator-(const Matrix& m1) const {
    Matrix m3 (r, c, 0);

    for(int i=0;i<z;++i) {
        m3.p[i] = p[i] - m1.p[i];
    }

    return m3;
}

Matrix Matrix::operator*(const Matrix& m1) const {
    Matrix m3 (r, m1.c,0 );
    double s = 0;
    if(c == m1.r)   {
        for(int i = 0; i < r; ++i) {
            for(int j = 0; j < m1.c; ++j) {
                for(int k = 0; k < m1.r; ++k) {
                    s += this->operator()(i,k)*m1(k,j);
                }

                m3.p[i * (m1.c) + j] = s;
                s = 0;
            }
        }

        return m3;
    } else {
        std::cout << "Matrices are not compatible";
    }
}

Matrix& Matrix::operator*=(const Matrix& m1) {
    *this = *this *m1;

    return *this;
}

std::size_t Matrix::rows() const {
    return r;
}

std::size_t Matrix::cols() const {
    return c;
}

std::ostream& operator<<(std::ostream& x, const Matrix& m1) {
    for( int i=0;i<m1.r;++i) {
        for(int j=0;j<m1.c;++j) {
            x << m1.p[i*m1.c+j] << "\t";
        }

        std::cout << std::endl;
    }

    return x;
}

std::istream& operator>>(std::istream& y, Matrix& m1) {
    for( int i=0;i<m1.r;++i) {
        for(int j=0;j<m1.c;++j) {
            y >> m1.p[i * m1.c + j];
        }
    }

    return y;
}

Browse More Popular Posts

Leave a Comment