预期 { 在输出末尾

Expected { at the end of output

本文关键字:输出 预期      更新时间:2023-10-16

我正在尝试运行过去几天的代码,但这些错误并没有消失。代码在代码块中运行平稳,但在 Linux 中生成错误。

错误是:

Matrix.h:14:20: error: expected ‘)’ before ‘rows’
Matrix(std::size_t rows, std::size_t cols, double initValue)
                   ^~~~
Matrix.h:234:2: error: expected ‘}’ at end of input
};
 ^
Matrix.h:10:20: error: expected unqualified-id at end of input
       double *p = nullptr;

我已经检查了分号和括号,它们似乎没问题。

我的代码是

#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
    private:
        int r, c, z;
        double *p = nullptr;
    public:
    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;
          //cout<< p[i];
      }
      }

    ~Matrix()
    {
        delete [] p;
    }
    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];
     }
    }
//= operator overloading
    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& operator()(std::size_t i, std::size_t j)
    {
       return p[i*c+j];
    }

    const double& operator()(std::size_t i, std::size_t j) const
    {
      return p[i*c+j];
    }
    bool 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 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& operator +=(const Matrix& m1)
    {
        for(int i=0;i<z;++i)
        {
           p[i]=p[i]+m1.p[i];
        }return *this;
    }
    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& operator -=(const Matrix& m1)
    {
        for(int i=0;i<z;++i)
        {
           p[i]=p[i]-m1.p[i];
        }return *this;
    }
    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 operator *(const Matrix& m1) const
    {
        Matrix m3 (r,m1.c,0);
        double s=0; //temp
        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& operator *=(const Matrix& m1)
    {
        /*Matrix m3 (r,m1.c,0);
        double s=0; //temp
        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;
            }
        }
        *this=m3;*/
        *this = *this *m1;
        return *this;
        }
    std::size_t rows() const
    {
        return r;
    }
    std::size_t cols() const
    {
        return c;
    }
    friend 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;
    }
    friend 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;
        }
    }
};
#endif // MATRIX_H

请让我知道我错在哪里或遗漏了什么!我正在尝试为不同的测试用例运行MatrixProduct.cpp。

提前谢谢。

因此,尽管将代码放入标头既快速又方便,但它几乎总是会导致编译错误,这些错误需要比创建标准 .h 和 .cpp 文件所需的时间更长的时间来跟踪,尤其是对于这么多代码。

我不确定问题出在哪里,但它可能是矩阵::operator>>((。 通过使用 IDE (如 eclipse(并按 Ctrl-i 自动缩进代码,您将能够找到此类错误。 另外,请检查我没有弄乱您的任何逻辑格式化它。

此外,还有一些关于矩阵内存分配的学术技巧问题。 如果我没记错的话,你应该一次分配所有内存,然后使用指针来引用数组元素。 我可能是错的,我现在不打算讨论它。 像这样:

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);
}
  • 请注意,我不知道矩阵数学是否正确

矩阵.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

矩阵.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;
}