C++赋值运算符动态数组

C++ assignment operators dynamic arrays

本文关键字:数组 动态 赋值运算符 C++      更新时间:2023-10-16

首先,我知道乘法部分是错误的,但我对代码有一些疑问。

1. 当我重载我的运算符+时,我使用 cout 打印出矩阵 <<*这然后在我返回 *this 之后,当我在 matix a 和 matix b 上做 a+b 时,它不会给我同样的东西,这非常令人困惑。

2. 当我在我的主矩阵中制作矩阵 c 时,由于某种原因我无法使用我的默认构造函数,因为当我使用我的赋值运算符重载函数设置它 = 时,它会给我一个错误,说"表达式必须是可修改的值。尽管使用设置行号和列号的构造函数与使用 (0,0) 的默认构造函数相同。

3. 我的赋值运算符=函数使用复制构造函数使用等号右侧的值创建一个新矩阵,当我打印出 c 时,它没有给我任何东西

任何帮助都会很棒,这是我的算法类的硬件,我仍然需要为乘法矩阵做算法,但我需要先解决这些问题,我遇到了很多麻烦,请帮忙。

//Programmer:   Eric Oudin
//Date:         10/21/2013
//Description:  Working with matricies
#include <iostream>
using namespace std;
class matrixType
{
public:
    friend ostream& operator<<(ostream&, const matrixType&);
    const matrixType& operator*(const matrixType&);
    matrixType& operator+(const matrixType&);
    matrixType& operator-(const matrixType&);
    const matrixType& operator=(const matrixType&);
    void fillMatrix();
    matrixType();
    matrixType(int, int);
    matrixType(const matrixType&);
    ~matrixType();
private:
    int **matrix;
    int rowSize;
    int columnSize;
};
ostream& operator<< (ostream& osObject, const matrixType& matrix)
{
    osObject << endl;
    for (int i=0;i<matrix.rowSize;i++)
    {
        for (int j=0;j<matrix.columnSize;j++)
        {
            osObject << matrix.matrix[i][j] <<", ";
        }
        osObject << endl;
    }
    return osObject;
}
const matrixType& matrixType::operator=(const matrixType& matrixRight)
{
    matrixType temp(matrixRight);
    cout << temp;
    return temp;
}
const matrixType& matrixType::operator*(const matrixType& matrixRight)
{
    matrixType temp(rowSize*matrixRight.columnSize, columnSize*matrixRight.rowSize);
    if(rowSize == matrixRight.columnSize)
    {
        for (int i=0;i<rowSize;i++)
        {
            for (int j=0;j<columnSize;j++)
            {
                temp.matrix[i][j] = matrix[i][j] * matrixRight.matrix[i][j];
            }
        }
    }
    else
    {
        cout << "Cannot multiply matricies that have different size rows from the others columns." << endl;
    }
    return temp;
}
matrixType& matrixType::operator+(const matrixType& matrixRight)
{
    matrixType temp;
    if(rowSize == matrixRight.rowSize && columnSize == matrixRight.columnSize)
    {
        temp.setRowsColumns(rowSize, columnSize);
        for (int i=0;i<rowSize;i++)
        {
            for (int j=0;j<columnSize;j++)
            {   
                temp.matrix[i][j] = matrix[i][j] + matrixRight.matrix[i][j];
            }
        }
    }
    else
    {
        cout << "Cannot add matricies that are different sizes." << endl;
    }
    return temp;
}
matrixType& matrixType::operator-(const matrixType& matrixRight)
{
        matrixType temp(rowSize, columnSize);
    if(rowSize == matrixRight.rowSize && columnSize == matrixRight.columnSize)
    {
        for (int i=0;i<rowSize;i++)
        {
            for (int j=0;j<columnSize;j++)
            {
                matrix[i][j] -= matrixRight.matrix[i][j];
            }
        }
    }
    else
    {
        cout << "Cannot subtract matricies that are different sizes." << endl;
    }
    return *this;
}
void matrixType::fillMatrix()
{
    for (int i=0;i<rowSize;i++)
    {
        for (int j=0;j<columnSize;j++)
        {
            cout << "Enter the matix number at (" << i << "," << j << "):";
            cin >> matrix[i][j];
        }
    }
}
matrixType::matrixType()
{
    rowSize=0;
    columnSize=0;
    matrix = new int*[rowSize];
    for (int i=0; i < rowSize; i++)
    {
        matrix[i] = new int[columnSize];
    }
}
matrixType::matrixType(int setRows, int setColumns)
{
    rowSize=setRows;
    columnSize=setColumns;
    matrix = new int*[rowSize];
    for (int i=0; i < rowSize; i++)
    {
        matrix[i] = new int[columnSize];
    }
}
matrixType::matrixType(const matrixType& otherMatrix)
{
    rowSize=otherMatrix.rowSize;
    columnSize=otherMatrix.columnSize;
    matrix = new int*[rowSize];
    for (int i = 0; i < rowSize; i++)
    {
        matrix[i]=new int[columnSize];
        for (int j = 0; j < columnSize; j++)
        {
            matrix[i][j]=otherMatrix.matrix[i][j];
        }
    }
}
matrixType::~matrixType()
{
    delete [] matrix;
}
int main()
{
    matrixType a(2,2);
    matrixType b(2,2);
    matrixType c(0,0);
    cout << "fill matrix a:"<< endl;;
    a.fillMatrix();
    cout << "fill matrix b:"<< endl;;
    b.fillMatrix();
    cout << a;
    cout << b;
    //c = a+b;
    cout <<"matrix a + matrix b =" << a+b;
    system("PAUSE");
    return 0;
}




编辑:
仍然遇到问题,无法返回我告诉它返回的内容

  1. 此代码不会执行您认为它执行的操作:

    for (int j = 0; j < columnSize; j++)
    {
        matrix[i]=new int[columnSize];
        matrix[i][j]=otherMatrix.matrix[i][j];
    }
    

    它分配一列,然后复制第一个值。然后它分配另一列(忘记旧列),并复制第二个值。然后它分配另一列(再次忘记旧列),并复制第三个值。

    我希望仅从这一描述中就清楚了问题。

  2. 你想用matrixType c();吗?这将声明一个函数。使用默认构造函数的正确语法是matrixType c;

  3. 您的运算符 = 实际上并没有分配任何内容。所以c = a+b;计算a+b但不改变c.