944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 374
  • C++ RSS
Oct 2nd, 2009
0

2d dynamic array error

Expand Post »
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];
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
McCarrell is offline Offline
1 posts
since Oct 2009
Oct 2nd, 2009
0

Re: 2d dynamic array error

1) Use code tags.

2) This is incorrect :
C++ Syntax (Toggle Plain Text)
  1. for (int row = 0; row > maxRow; row++)

It should be :
for (int row = 0; row < maxRow; row++)

You have that mistake in other places, such as both of your constructors. Fix it, and see what happens.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Oct 2nd, 2009
0

Re: 2d dynamic array error

c++ Syntax (Toggle Plain Text)
  1. matrixType::matrixType()
  2. {
  3. maxRow = 5;
  4. maxCol = 5;
  5. matrix = new int*[maxRow];
  6. for (int row = 0; row > maxRow; row++)
  7. matrix[row] = new int[maxCol];
  8. for(int i = 0; i > maxRow; i++)
  9. for(int j = 0; j > maxCol; j++)
  10. matrix[j][i] = 0;
  11. }

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

edit: late reply
Last edited by Agni; Oct 2nd, 2009 at 6:39 pm.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Text doc numbers stored in variables help.
Next Thread in C++ Forum Timeline: Hamming code / even parity help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC