I need help to creater allocate space.
Here is what I have
class Array
{
friend ostream &operator<<( ostream &, const Array & );
friend istream &operator>>( istream &, Array & );
public:
Array( int = 10,int =10 );
Array( const Array & );
~Array();
// inequality operator; returns opposite of == operator
bool operator!=( const Array &right ) const
{
return ! ( *this == right ); // invokes Array::operator==
} // end function operator!=
// subscript operator for non-const objects returns modifiable lvalue
int &operator[]( int );
// subscript operator for const objects returns rvalue
int operator[]( int ) const;
private:
int size, m,n; // pointer-based array size
int a[10][10]; // pointer to first element of pointer-based array
}; // end class Array
class Array
{
friend ostream &operator<<( ostream &, const Array & );
friend istream &operator>>( istream &, Array & );
public:
Array( int = 10,int =10 );
Array( const Array & );
~Array();
// inequality operator; returns opposite of == operator
bool operator!=( const Array &right ) const
{
return ! ( *this == right ); // invokes Array::operator==
} // end function operator!=
// subscript operator for non-const objects returns modifiable lvalue
int &operator[]( int );
// subscript operator for const objects returns rvalue
int operator[]( int ) const;
private:
int size, m,n; // pointer-based array size
int a[10][10]; // pointer to first element of pointer-based array
}; // end class Array
To copy to clipboard, switch view to plain text mode
My code so far
#include <iostream>
#include <iomanip>
using namespace std;
#include "array.h"
Array::Array( int m, int n )
{
size = ( m*n > 0 ? m*n : 10 ); // validate arraySize
a[m][n] =*new int[size]; // create space for pointer-based array
for ( int i = 0; i < m ;i++ )
for ( int j = 0; j <n; j++ )
a[ i ][j] = 0; // set pointer-based array element
}
Array::~Array()
{
delete *[]a;
}
Array::Array( const Array &arrayToCopy )
: size( arrayToCopy.size )
{
a[m][n]= *new int[ size ];
for (int i = 0; i < n; i++ )
for (int j = 0; i < m; j++ )
a[ i ][j] = arrayToCopy.a[ i ][j];
}
int main()
{
Array mat1;
Array mat2(4,5);
}
#include <iostream>
#include <iomanip>
using namespace std;
#include "array.h"
Array::Array( int m, int n )
{
size = ( m*n > 0 ? m*n : 10 ); // validate arraySize
a[m][n] =*new int[size]; // create space for pointer-based array
for ( int i = 0; i < m ;i++ )
for ( int j = 0; j <n; j++ )
a[ i ][j] = 0; // set pointer-based array element
}
Array::~Array()
{
delete *[]a;
}
Array::Array( const Array &arrayToCopy )
: size( arrayToCopy.size )
{
a[m][n]= *new int[ size ];
for (int i = 0; i < n; i++ )
for (int j = 0; i < m; j++ )
a[ i ][j] = arrayToCopy.a[ i ][j];
}
int main()
{
Array mat1;
Array mat2(4,5);
}
To copy to clipboard, switch view to plain text mode
When I run it, it runs ok . But if I implement the destructor , it crashes. I think my memory allocation for a is probably screwy too.. Helpl
Bookmarks