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.

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...

could you give me an example?

for example...

...
void TwoD::input/*or whatever you wanna call it*/(){
   int n;
   cin>>n;
   d1=n;
}

you know how to manipulate functions inside classes?

// ...
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 ) ;
  //...
}
commented: precise!!! :D +3

precisely...

hope you solved your problem...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.