预期 { 在输出末尾
Expected { at the end of output
我正在尝试运行过去几天的代码,但这些错误并没有消失。代码在代码块中运行平稳,但在 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;
}
相关文章:
- 未获得字符串中的预期输出
- 为什么这个程序的输出不如预期
- 简单函数并不总是返回预期的输出
- 程序打印 % 后预期输出
- 我的求解(字符串 a、字符串 b)的输出与随机哈希中的预期输出不匹配
- Fmod 函数清楚地输出一个预期的双精度值,但 if(fmod == 预期的双精度值)的计算结果不是 true
- 使用 Node.js N-API 调用 C 函数时,不会显示预期的输出
- 在C++中未从代码接收预期输出
- 存储和打印字符,但输出不是预期的
- 为什么 boost::locale::to_title 不返回预期的输出?
- 为什么带有逻辑和运算符以及增量运算符的 c++ 程序给出的输出与预期不同?
- 预期输出之后的所有这些数字是多少
- 运行时错误,尽管我得到了预期的输出
- u张量模型输出不等于预期输出
- 未获得 N-皇后问题的预期输出
- C++ - 未获得控制台的预期输出
- 使用 wstring 和 wcout 无法获得预期的输出
- 为什么我的函数没有输出预期的内容
- 计算未输出预期答案
- 如果.还.语句不输出预期结果.而.循环问题