| | |
2d dynamic array error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
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];
}
}
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];
}
}
1) Use code tags.
2) This is incorrect :
It should be :
You have that mistake in other places, such as both of your constructors. Fix it, and see what happens.
2) This is incorrect :
C++ Syntax (Toggle Plain Text)
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.
I give up!
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer? c++ Syntax (Toggle Plain Text)
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
Last edited by Agni; Oct 2nd, 2009 at 6:39 pm.
thanks
-chandra
-chandra
![]() |
Similar Threads
- Dynamic array => dropdown issues... (PHP)
- get length of a dynamic array (C++)
- Dynamic Array Help (C)
- Error when calling a method of dynamic array (C++)
- How sort a Dynamic Array? (C++)
- Dynamic array management (C)
- works for static need help to make it dynamic (C++)
- dynamic array of structures problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Text doc numbers stored in variables help.
- Next Thread: Hamming code / even parity help
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






