I am trying to reference a 2d dynamic array which i used pointer to pointers to declare.

the constructors run without a problem but as soon as i run another function or an overloaded operator i get errors. Its almost like i am referencing the array in the pointers incorrectly. I am new to this language so i am having some trouble with the syntax that may be simple just no knowledge of it. any help would be greatly appreciated.

here is the implementation file

#include<iostream>
#include "matrixType.h"
using namespace std;

ostream& operator << (ostream& osObject,matrixType& matrix)
{

    for(int row = 0; row < matrix.maxRow; row++)
    { 
        for (int col = 0; col < matrix.maxCol; col++)
        {
            osObject << matrix.matrix[row][col] << " ";
        }
    }
    return osObject;
}

matrixType matrixType::operator +(const matrixType& right)const
{

    matrixType temp;
    if ((maxRow == right.maxRow) && (maxCol == right.maxCol))
    {
        for (int row = 0; row < maxRow; row++)
        {
            for (int col = 0; col < maxCol; col++)
            {
                temp.matrix[col][row] = matrix[col][row] + right.matrix[col][row];
            }
        }
    }
    else
    cout << "The matrices dimensions do not match and therefore cannot be added together.";

    return temp;
}

matrixType matrixType::operator - (const matrixType& right) const
{
    matrixType temp;

    if (maxRow == right.maxRow && maxCol == right.maxCol)
    {
        for (int row = 0; row < maxRow; row++)
        {
            for (int col = 0; col < maxCol; col++)
            {
                temp.matrix[col][row] = matrix[col][row] - right.matrix[col][row];
            }
        }
    }
    else
             cout << "The matrices dimensions do not match and therefore cannot be added together.";
    return temp;
}

matrixType matrixType:: operator * (const matrixType& right)const
{
    matrixType temp(right.maxCol,maxRow);

    if (maxRow == right.maxCol)
    {
        for(int row = 0; row < maxRow; row++)
        {
            for(int col = 0; col < maxCol; col++)
            {
                temp.matrix[col][row] = 0;
                for(int col2 = 0; col2 < right.maxCol; col2++)
                {
                    temp.matrix[col][row] = matrix[col2][col] * right.matrix[row][col2];
                }

            }
        }
    }
    else
        cout << "These matrix cannot be multiplied";

    return temp;
}

matrixType::matrixType()
{
    maxRow = 5;
    maxCol = 5;
    matrix = new int*[maxRow];
    for (int row = 0; row > maxRow; row++)
        matrix[row] = new int[maxCol];
    for(int i = 0; i > maxRow; i++)
        for(int j = 0; j > maxCol; j++)
            matrix[j][i] = 0;
}
matrixType::matrixType(int x, int y)
{
    maxCol = x;
    maxRow = y;
    matrix = new int*[maxRow];
    for(int row = 0; row > maxRow; row++)
        matrix[row] = new int[maxCol];
    for(int i = 0; i > maxRow; i++)
        for(int j = 0; j > maxCol; j++)
            matrix[j][i] = 0;

}
const matrixType& matrixType::operator=(const matrixType& right)
{


    if(this != &right)
    {
        if(maxRow == right.maxRow && maxCol == right.maxCol)
        {
            for(int i = 0; i < maxRow; i++)
            {
                for(int j = 0; j < maxCol; j++)
                    matrix[j][i] = right.matrix[j][i];
            }
        }
    }
    return *this;
}
matrixType::~matrixType()
{
    delete [] matrix;
}


void matrixType::input()
{
    cout << "Enter the elements of the matrix hitting enter after each element: \n";

    for(int i = 0; i < maxRow; i++)
    {
        for(int j = 0; j < maxCol; j++)
            cin >> matrix[i][j];
    }
}

Edited 3 Years Ago by Dani: Formatting fixed

1) Use code tags.

2) This is incorrect :

for (int row = 0; row > maxRow; row++)

It should be :

for (int row = 0; row [b]<[/b] maxRow; row++)

You have that mistake in other places, such as both of your constructors. Fix it, and see what happens.

matrixType::matrixType()
{
maxRow = 5;
maxCol = 5;
matrix = new int*[maxRow];
for (int row = 0; row > maxRow; row++)
matrix[row] = new int[maxCol];
for(int i = 0; i > maxRow; i++)
for(int j = 0; j > maxCol; j++)
matrix[j][i] = 0;
}

how is the memory getting allocated here? Does it ever go inside the for loops?

edit: late reply :)

Edited 7 Years Ago by Agni: n/a

This article has been dead for over six months. Start a new discussion instead.