简体   繁体   English

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

[英]C++ assignment operators dynamic arrays

First off i know the multiplying part is wrong but i have some questions about the code. 首先,我知道乘法部分是错误的,但是我对代码有一些疑问。

1. When i am overloading my operator+ i print out the matrix using cout << *this then right after i return *this and when i do a+b on matix a and matix b it doesnt give me the same thing this is very confusing. 1.当我重载我的运算符+时,我使用cout << * this打印出矩阵,然后在我返回* this之后,当我在matix a和matix b上执行a + b时,它并没有给我同样的东西,这非常令人困惑。

2. When i make matrix c down in my main i cant use my default constructor for some reason because when i go to set it = using my assignment operator overloaded function it gives me an error saying "expression must be a modifiable value. although using my constructor that sets the row and column numbers is the same as my default constructor using (0,0). 2.当我在我的主数组中放下矩阵c时,由于某种原因,我不能使用默认的构造函数,因为当我去设置它时=使用我的赋值运算符重载函数,它给我一个错误,指出“表达式必须是可修改的值。尽管使用设置行号和列号的构造函数与使用(0,0)的默认构造函数相同。

3. My assignment operator= function uses a copy constructor to make a new matrix using the values on the right hand side of the equal sign and when i print out c it doesn't give me anything 3.我的赋值操作符=函数使用复制构造函数使用等号右侧的值创建一个新矩阵,当我打印出c时,它什么也没给我

Any help would be great this is my hw for a algorithm class which i still need to do the algorithm for the multiplying matrices but i need to solve these issues first and im having a lot of trouble please help. 任何帮助都会很棒,这是我为算法类​​编写的硬件,我仍然需要对乘法矩阵进行算法处理,但我需要首先解决这些问题,请给我带来很多麻烦。

//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;
}




EDIT: 编辑:
still having trouble with things not returning what i am telling it to return 仍然有事情不返回我告诉它返回的麻烦

  1. This code doesn't do what you think it does: 这段代码无法完成您认为的操作:

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

    It allocates a column, then copies the first value. 它分配一列,然后复制第一个值。 Then it allocates another column (forgetting about the old column), and copies the second value. 然后,它分配另一列(忘记旧列),并复制第二个值。 Then it allocates another column (forgetting about the old column again), and copies the third value. 然后,它分配另一列(再次忘记旧列),并复制第三个值。

    I hope the problem is clear from that description alone. 我希望仅从该描述中就能解决问题。

  2. Are you trying to use matrixType c(); 您是否要使用matrixType c(); ? That would declare a function. 那将声明一个函数。 The correct syntax to use the default constructor would be matrixType c; 使用默认构造函数的正确语法为matrixType c;

  3. Your operator= doesn't actually assign anything. 您的operator =实际上没有分配任何东西。 So c = a+b; 所以c = a+b; calculates a+b but doesn't change c . 计算a+b但不改变c

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

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