hi guys,
I want to create a class called DoubleSubScriptedArray.this array should be able to take maybe a 3 by 5 array or 1 by 3.My program is giving me a problem when i try to overload the stream-insertion and stream-extraction operator.Below is my program pliz help me to get rid of thge errors:
#ifndef DOUBLESUBSCRIPTEDARRAY_H
#define DOUBLESUBSCRIPTEDARRAY_H
#include <iostream>
using namespace std;
// class DoubleScriptedArray definition
class DoubleSubscriptedArray {
friend ostream &operator<<(ostream &,const DoubleSubcriptedArray &);
friend istream &operator>>( istream &, DoubleSubcriptedArray & );
public:
DoubleSubcriptedArray( int = 10, int = 10 );
DoubleSubcriptedArray( const DoubleSubcriptedArray & );
~DoubleSubcriptedArray();
const DoubleSubscriptedArray &operator=( const DoubleSubscriptedArray &)
bool operator==( const DoubleSubcriptedArray & ) const;
bool operator!=( const DoubleSubscriptedArray &right)
{
return ! ( *this == right );
} // end function operator!=
int &operator()( int, int ); // lvalue
const int &operator(int,int)const; //prototype for overloaded()
static int getArrayCount();
private:
int rows; // number of rows in array
int columns; // number of columns in array
int *ptr; // pointer to first element of array
static int arraycount;
}; // end class DoubleScriptedArray
#endif
#include <iostream>
using std::ostrstream;
using std::istrstream;
using namespace std;
#include <iomanip>
using std::setw;
#include <cstdlib>
#include <new>
#include <cassert>
#include "DoubleSubscriptedArray.h"
int arrayCount = 0;
// constructor
DoubleSubcriptedArray::DoubleScriptedArray( int r, int c )
{
rows = ( r > 0 ? r : 10 );
columns = ( c > 0 ? c : 10 );
ptr = new int[ rows * columns ];
for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = 0;
} // end class DoubleScriptedArray constructor
// copy constructor
DoubleSubcriptedArray::DoubleSubcriptedArray( const DoubleSubcriptedArray &init )
{
int init;
rows = init.rows;
columns = init.columns;
ptr = new int[ rows * columns ];
for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = init.ptr[ i ];
} // end class DoubleSubcriptedArray copy constructor
//destructor
DoubleSubscriptedArray::~DoubleSubscriptedArray()
{
delete [] ptr;
--arrrayCount;
}
//definition for operator =
const DoubleSubscriptedArray &DoubleSubscriptedArray operator=(const DoubleSubscriptedArray &right)
{
if(&right != this){
if(rows != right.rows || columns != right.columns){
delete [] ptr;
rows = right.rows;
columns = right.columns;
prt = new int [rows * columns];
assert (ptr != 0);
}
for ( int a = 0; a < rows * columns; a++)
ptr[a] = right.ptr [a];
}
return *this;
}
// function operator== definition
bool DoubleSubcriptedArray::operator==( const DoubleSubcriptedArray &right ) const
{
if ( rows != right.rows || columns != right.columns )
return false;
for ( int i = 0; i < rows * columns; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false;
return true;
} // end function operator==
// overloaded subscript operator for non-const Arrays
// reference return creates an lvalue
int &DoubleSubcriptedArray::operator()( int d, int e )
{
if ( !( d > 0 && e < rows ) )
d = 0;
if ( !( d > 0 && e < columns ) )
e = 0;
return ptr[ ( columns * d + e ) ];
} // end function operator()
// overloaded sububscript operator for const Arrays
// const reference return creates an rvalue
const int &DoubleSubcriptedArray::operator()( int d, int e ) const
{
if ( !( d > 0 && e < rows ) )
d = 0;
if ( !( d > 0 && e < columns ) )
e = 0;
return ptr[ ( columns * d + e ) ];
} // end function operator()
//Return the number of Array objects instantiated
int DoubleSubscriptedArray::getarrayCount()
{
return arrayCount;
}
// function operator>> definition
istream &operator>>( istream &input, DoubleSubcriptedArray &p )
{
for ( int a = 0; a < p.rows * p.columns; a++ )
input >> p.ptr[ a ];
return input;
} // end function operator>>
// function operator<< definition
ostream &operator <<(ostream &output, const DoubleSubscriptedArray &p )
{
for ( int i = 0; i < a.rows * a.columns; i++ ) {
output << setw( 6 ) << a.ptr[ i ];
if ( ( i + 1 ) % a.columns == 0 )
output << endl;
} // end for
if ( i % a.columns != 0 )
output << endl;
return output;
} // end function operator<<
#include <iostream>
using std::istream;
using std::ostream;
#include <ctime>
#include "DoubleSubscriptedArray.h"
int main()
{
// seed rand function
srand( time( 0 ) );
// create two arrays
DoubleScriptedArray Pack1( 6, 7);
DoubleScriptedArray Pack2( 8, 2);
cout << "Uninitialized array \"Pack1\" is: \n" << Pack1
<< "Uninitialized array \"Pack2\" is: \n" << Pack2;
// initialize array "a" with random values (0-100)
for ( int a = 0; a < 6; a++ )
for ( int b = 0; b < 7; b++ ){
output<<"Enter array elements : ";
input>>Pack1;
Pack2 = Pack1;
output<< "\nInitialized array \"Pack1\" is now:\n" << Pack1
<< "Assigning Pack2 = Pack1:\n" << Pack2;
// check if arrays are equal using overloaded ==
if ( Pack1 == Pack2 )
output<<"\"Pack1\" was found to be equal to \"Pack2\"\n";
else
output<<"\"Pack1\" was found to be not equal to \"Pack2\"\n";
// retrieve array element using overloaded operator()
output<<"The element (2, 1) of array \"Pack1\" is: "
<<Pack1( 2, 1 )<< endl;
// change element of array using overloaded operator()
Pack1( 2, 1 ) = -1;
output<<"Changed element (2, 1) to -1: \n" << Pack1;
// check if arrays are still equal
if ( Pack1==Pack2 )
output<<"\"Pack1\" was found to be equal to \"Pack2\"\n";
else
output<<"\"Pack1\" was found to be Not equal to \"Pack2\"\n";
return 0;
} // end main
:eek: