2017-11-11 105 views
-5

我试图从过去几天运行我的代码,但这些错误只是不会消失。代码在代码块中平稳运行,但在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。

在此先感谢。

+2

您在ge中的缩进和格式neral似乎很随意。尽量保持一致,这将使代码更易于阅读。 –

+0

另外,你在哪里包括头文件?在*#include之前,你在做什么?你在用C++ 11(或更高版本)编译器构建吗? –

+0

我有一些其他的源代码必须包含这个头文件。 –

回答

0

因此,尽管将代码放入标题中速度快捷方便,但它几乎总是会导致编译错误,这些编译错误需要比创建标准的.h和.cpp文件所需的时间更长的跟踪时间,特别是有这么多的代码。

我不确定问题出在哪里,但它可能是Matrix :: operator >>()。通过使用像eclipse这样的IDE并按下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); 
} 
  • 请注意,我不知道如果矩阵数学是正确的

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; 
} 
+0

您的答案与OP的代码具有相同的缺陷。如果有人现在要复制该代码,并认为它有效,他们会感到失望。您应该放弃免责声明 - “自担风险使用”。 – PaulMcKenzie

+0

所以,我不知道OP是什么 - 如果你指的是原始问题 - 我没有阅读代码,所以我相信你是对的 –

相关问题