简体   繁体   中英

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.

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).

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

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(); ? That would declare a function. The correct syntax to use the default constructor would be matrixType c;

  3. Your operator= doesn't actually assign anything. So c = a+b; calculates a+b but doesn't change c .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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