implementing a two demensional array class
Hey guys, Could you tell me what is wrong with this part of my code:
#include <iostream>
using namespace std ;
class TwoD
{
public:
TwoD ( ) ;
TwoD ( int , int ) ;
private:
int *m ;
int d1 ;
int d2 ;
};
int main ( )
{
int d1 , d2 , j , k ;
cout << "Enter row and column dimensions: " << flush ;
cin >> d1 >> d2 ;
TwoD m1 ( d1 , d2 ) ;
cout << Enter " << d2 << " rows of " << d2 << " doubles each\n"
for ( k = 0 ; k < d1 ; k++ )
for ( j = 0 ; j < d2 ; j++ )
cin >> m1 ( k , j ) ;
...
return 0 ;
}
I get an error that says
Term does not evaluate to a function taking 2 arguments
This happens at the last line of the code you see.
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
it must be your ml(int,int) is not a variable where you can input data... there should be a public function that reads your parameters to take it into your variable...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
could you give me an example?
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
for example...
...
void TwoD::input/*or whatever you wanna call it*/(){
int n;
cin>>n;
d1=n;
}
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
you know how to manipulate functions inside classes?
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
// ...
TwoD m1 ( d1 , d2 ) ;
// ...
cin >> m1 ( k , j ) ;
// ...
for this to work, the class TwoD should provide an overloaded function call operator that can take k and j as args. and would return a reference to the 2d array element at k,j as a modifiable l-value. eg.
#include <iostream>
#include <cassert>
using namespace std ;
class TwoD
{
public:
// ...
TwoD ( int nrows, int ncols ) : d1(nrows), d2(ncols)
{
assert( d1>0 && d2>0 ) ;
m = new int[ d1 * d2 ] ;
}
int operator() ( int i, int j ) const
{ assert( i<d1 && j<d2 ) ; return ( m + i*d2 )[j] ; }
int& operator() ( int i, int j )
{ assert( i<d1 && j<d2 ) ; return ( m + i*d2 )[j] ; }
// ...
private:
int *m ;
int d1 ;
int d2 ;
};
using a function call operator to access array elements would not be intuitive to most c++ programmers; overloading the array subscript operator would make the use of TwoD easier. eg.
#include <iostream>
#include <cassert>
using namespace std ;
class TwoD
{
public:
// ... ;
TwoD ( int nrows, int ncols ) : d1(nrows), d2(ncols)
{
assert( d1>0 && d2>0 ) ;
m = new int[ d1 * d2 ] ;
}
struct row ;
const row operator[] ( int i ) const
{ assert( i<d1 ) ; return row( m + i*d2, d2 ) ; }
row operator[] ( int i )
{ assert( i<d1 ) ; return row( m + i*d2, d2 ) ; }
struct row
{
int operator[] ( int j ) const
{ assert( j<n ) ; return begin[j] ; }
int& operator[] ( int j )
{ assert( j<n ) ; return begin[j] ; }
private:
row( int* r, int nn ) : begin(r), n(nn) {}
int* begin ;
int n ;
friend row TwoD::operator[](int) ;
friend const row TwoD::operator[](int) const ;
};
// ...
private:
int *m ;
int d1 ;
int d2 ;
};
int main()
{
int d1 , d2 , j , k ;
cin >> d1 >> d2 ;
TwoD m1 ( d1 , d2 ) ;
for ( k = 0 ; k < d1 ; k++ )
for ( j = 0 ; j < d2 ; j++ )
cin >> m1[k][j] ; // ( k , j ) ;
//...
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
precisely...
hope you solved your problem...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57