简体   繁体   English

运算符+类的C ++重载

[英]Operator + overload for c++ in class

This is for assignment in my faculty, I want to do overload for operator + to matrix I create new matrix an put the sum in this matrix and I print it but it print nothing so can anyone help me to solve this problem? 这是为我的老师分配的,我想对operator +进行矩阵重载,以创建新矩阵并将总和放入此矩阵中,然后将其打印出来,但没有打印出来,所以有人可以帮助我解决这个问题吗?

class matrix
{
private:
  int* data;
  int row, col;

public:
  matrix();
  matrix(int r, int c, int num[]);
  friend ostream& operator << (ostream& out, matrix mat1);
  friend istream& operator >> (istream& in, matrix& mat);
  int getrow();
  int getcol();
  int getdata(int i);
  int setrow(int r);
  int setcol(int c);
  int setdata(int num[]);
  matrix operator+ (const matrix& mat2);
};

Now I write this code an it prints nothing can anyone help me to solve this? 现在,我编写此代码,它不会打印任何内容,有人可以帮助我解决此问题吗? first I make setter and getter for the data. 首先,我对数据进行设置和获取。

matrix::matrix(){
  row=0;
  col=0;
}

matrix::matrix(int r, int c, int num[])
{
  row = r;
  col = c;
  data = new int[r*c];
  for(int i=0; i < (r*c); i++)
    data[i] = num[i];
}

matrix::getrow() { return row; }
matrix::getcol() { return col; }

matrix::setrow(int r) { row=r; }
matrix::setcol(int c) { col=c; }

matrix matrix::operator+ (const matrix& mat2)
{
  matrix mat3;
  for(int i=0; i < mat3.getrow() * mat3.getcol(); i++) {
    mat3.data[i] = data[i] + mat2.data[i];
  }
  return mat3;
}

int main(){
  int num2 [] = {1,2,3,4,5,6,7,8,8};
  int num3 [] = {1,0,0,0,1,0,0,0,1};

  matrix x;

  matrix mat2(3, 3, num2);
  matrix mat3(3, 3, num3);
  matrix mat4;
  mat4 = mat2 + mat3;
  cou << mat4;

  return 0;
}

You did not set the dimensions of mat3 , so getrow() and getcol() both return 0 , and the buffer is too small to contain the data you put it in, so you also suffer from buffer overflow. 您没有设置mat3的尺寸,因此getrow()getcol()都返回0 ,并且缓冲区太小而无法容纳放入其中的数据,因此,您还会遭受缓冲区溢出的困扰。

Add a private constructor that only accepts the dimensions without initializing values. 添加一个仅接受维度而不初始化值的私有构造函数。

Your next problem is that you don't have a copy/move constructor/operator=(), so the assignment into mat3 in main() is not well defined. 下一个问题是您没有复制/移动构造函数/ operator =(),因此对main() mat3的赋值定义不明确。

Here are some suggestions 这里有一些建议

1. Initialization mistake 1.初始化错误

try to add a constructor overload method like this: 尝试添加一个像这样的构造函数重载方法:

matrix::matrix(int r, int c)
{
    row = r;
    col = c;
    data = new int[r*c];
    for (int i = 0; i < r*c; i++) {
        data[i] = 0;
    }
}

then in operator+ overloading function, initiate mat3 by using this method: 然后在operator+重载功能中,使用以下方法启动mat3:

matrix mat3(mat2.getrow(), mat2.getcol());


2.overload = operator is essential since you decide to mat4 = 2.overload =运算符至关重要,因为您决定使用mat4 =

matrix matrix::operator= (const matrix& from_mat)
{
    matrix to_mat(from_mat.getrow(), from_mat.getcol());
    for (int i = 0; i < from_mat.getrow() * from_mat.getcol(); i++) {
        to_mat.data[i] = from_mat.data[i];
    }
    return to_mat;
}


3. in main function, just use matrix mat4 = mat2 + mat3; 3.在main函数中,只需使用matrix mat4 = mat2 + mat3;

There are many things wrong with your post. 您的帖子有很多错误。 Others have pointed a few things out, here is a more complete list IMO: 其他人指出了一些事情,这是IMO的完整列表:

  • You are asking about a display issue, but your title is about operator+ , and you do not provide the implementation of ostream& operator<< ( ostream&, matrix& ) . 您在询问显示问题,但标题是关于operator+ ,并且您没有提供ostream& operator<< ( ostream&, matrix& ) There is also no reason why displaying the contents of a matrix should modify it, so it should be ostream& operator<< ( ostream&, const matrix& ) (this will also eliminate the need for the pass-by-value variant). 也没有理由要显示矩阵的内容来对其进行修改,因此应为ostream& operator<< ( ostream&, const matrix& ) (这也将消除传递值变体的需要)。
  • Some of your constructors allocate memory, but you did not define a destructor ( ~matrix() ) to handle deallocation, and have no copy constructor ( matrix( const matrix& ) ). 您的一些构造函数会分配内存,但是您没有定义析构函数( ~matrix() )来处理释放,并且没有复制构造函数( matrix( const matrix& ) )。 Read about destructors and memory management. 了解有关析构函数和内存管理的信息。
  • You overload the default constructor matrix() but it is currently not possible to update the size or contents of your matrix. 您重载了默认的构造函数matrix()但当前无法更新矩阵的大小或内容。 What is the point of a default constructor if the empty matrix it creates cannot be changed later on? 如果默认构造函数创建的空矩阵以后无法更改,那又有什么意义呢? You should also define data = nullptr in this case. 在这种情况下,您还应该定义data = nullptr
  • The role of the setters for number of rows and columns ( setrow, setcol ) is unclear; 设置器在行和列数( setrow, setcol )中的作用尚不清楚; setting the size attributes should affect the memory allocation (ie data ), but currently it does not. 设置大小属性应该会影响内存分配(即data ),但目前不会。 You also declare those methods as returning int s, but do not return anything. 您还可以将这些方法声明为返回int ,但不返回任何内容。 You should delete both of these methods; 您应该删除这两种方法。 there is no need for them, and your implementation is currently wrong. 不需要它们,您的实现当前是错误的。
  • In operator+ the first thing to check is that the input has the same size as the current matrix. operator+ ,首先要检查的是输入的大小与当前矩阵的大小相同。 The second thing is to allocate the output correctly (with matrix( row, col ) ), which implies the need for an additional constructor matrix(int r, int c) (as suggested by the other answers). 第二件事是正确分配输出(使用matrix( row, col ) ),这意味着需要附加的构造函数matrix(int r, int c) (如其他答案所建议)。 The third is to declare this method as constant matrix operator+ ( const matrix& other ) const . 第三是将这种方法声明为常量matrix operator+ ( const matrix& other ) const
  • There is a typo in your main, cou should be std::cout . 您的主目录中有一个错字, cou应该是std::cout
  • Finally, what happens if I write matrix x( -1, -5, NULL ); 最后,如果我写matrix x( -1, -5, NULL );会发生什么matrix x( -1, -5, NULL ); ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM