标题:想问问为啥错了。。想不明白
取消只看楼主
LRLIG
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2019-12-17
结帖率:0
已结贴  问题点数:20 回复次数:1 
想问问为啥错了。。想不明白
#ifndef CLASS_H
#define CLASS_H

#include <iostream>
#include <cstdlib>    //(rand()%10)
#include <ctime>
using namespace std;

class Matrix
{
private:
    int row;
    int column;
    int* data;

public:
    Matrix(int row2, int column2);
    Matrix();
    ~Matrix();
    Matrix operator+ (const Matrix& a) const;
    Matrix operator- (const Matrix& a) const;
    Matrix operator* (const Matrix& a) const;
    friend ostream& operator<< (ostream& out, Matrix& a);

};

Matrix::Matrix(int row2, int column2)
{
    srand((int)time(0));
    row = row2;
    column = column2;
    int i = 0, x = 0;

    int** p;
    p = new int* [row2]; //行 //申请行的空间
    //每行的列申请空间
    for (i = 0; i < row; i++)
    {
        p[i] = new int[column];
    }
   
    while (i < row2)
    {
        while (x < column)
        {
            p[i][x]=rand()%10;
            x++;
        }
        i++;
    }
    data = p[row2];
}

Matrix::Matrix()
{
    row = 0;
    column = 0;
    data = NULL;
}

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



Matrix Matrix::operator+ (const Matrix& a) const
{
    Matrix A(row, column);
    int i = 0,x =0;
    while (i < row )
    {
        while (x < 4)
        {
            A.data[i][x] = data[i][x] + a.data[i][x];
            x++;
        }
        i++;
    }
    return A;
}

Matrix Matrix::operator- (const Matrix& a) const
{
    Matrix B(row, column);
    int i = 0,x = 0;
    while (i < row)
    {
        while (x < column)
        {
            B.data[i][x] = data[i][x] + a.data[i][x];
            x++;
        }
        i++;
    }
    return B;
}

Matrix Matrix::operator* (const Matrix& a) const
{
    Matrix C(row, a.column);
    int i = 0, x = 0;
    while (i < row)
    {
        while (x < column)
        {
            C.data[i][x] = data[row][x] * a.data[i][a.column];
            x++;
        }
        i++;
    }
    return C;
}

ostream& operator<< (ostream& out, Matrix& a)
{
    int i = 0;
    while (i < a.row * a.column)
    {
        out << *(a.data + i) << "   ";
        if (a.column / 4 + 1 == a.column)          /**/
        {
            out << endl;
        }
    }
    return out;
}

#endif
搜索更多相关主题的帖子: data int Matrix const row 
2020-11-18 00:13
LRLIG
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2019-12-17
得分:0 
有大佬解答吗
2020-11-18 00:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503923-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.089323 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved