i have been working on this all DAY LONG. i am new to C++ and Unix and need to write this little tiny code in both. i am tripping all over myself with references and dereferences and operator overloads .... if anyone could help me decipher what my problem(s) are, that would be a lifesaver right now.
this is the error that i keep getting ... and below is the code.

classroom(52)% g++ lab2aa.C
lab2aa.C: In function `int size()':
lab2aa.C:55: `d_num_elements' undeclared (first use this function)
lab2aa.C:55: (Each undeclared identifier is reported only once for each
function it appears in.)
lab2aa.C: At global scope:
lab2aa.C:59: syntax error before `&' token
lab2aa.C: In member function `void IntArray::init(const int*, int)':
lab2aa.C:82: no match for `IntArray& [int&]' operator
lab2aa.C:85: no match for `IntArray& [int&]' operator

#include <iostream>
using namespace std;

#include "/unc/hedlund/121/lib/cpluslib.h"

class IntArray{


public:
        IntArray ( int sz = 10 );      
 
        IntArray ( const IntArray &x);

        virtual ~IntArray();

        int size () const;
    
        int &operator = ( int i ) const;

protected:
        int d_num_elements;
        int *d_array;


private:
        void init (const int *x, int sz);
};

IntArray::
IntArray  ( int sz )
{
        cout << "!!! default constructor called " <<endl;
        init ( NULL, sz);
}

IntArray::
IntArray ( const IntArray &x )
{
        cout<< "!!!  constructor" <<endl;
        init ( x.d_array, x.size() );
}

IntArray::
~IntArray()
{
        cout << "!!! IntArray destructor " <<endl;
        if ( size() > 0){
                delete [] d_array;
        }
}

int
size ()
{
        return (d_num_elements);
}

int IntArray::
&operator [] ( int i ) const
{
        return d_array[i];
}

void IntArray::
init ( const int *data, int sz)
{
        d_num_elements = sz;
        if ( 0 == d_num_elements ){                  // if no elements in array
                d_array = NULL;
        }
        else{
                d_array = new int [d_num_elements];
                                                     // allocate memory
                                                     // if new returns NULL then no
                                                     // more available memory
        }
        for (int i=0; i < sz; i++ ){
                                            // Loop Invariant:
                                            // for all j:0 .. i-1, self[j] == 0 OR
                                            //    self[j] == data[j]
                if ( NULL == data ){
                        self [i] = 0;
                }
                else{
                        self[i] = data[i];
                }
        }
}


int main () {
        IntArray a(5);
        IntArray b(8);
  cout << a.size() <<endl;
  cout << b.size() <<endl;
return(0); 
}
[\code]

thanks so much for looking at this!!

crq

Recommended Answers

All 3 Replies

your implementation of int size() has no class before it; it should be

int IntArray::size()

and int IntArray::&operator [] ( int i ) const

does not exist in your class definition (instead you have an = operator that you don't implement), and the '&' doesn't belong before the word 'operator'.

Good Luck!

your implementation of int size() has no class before it; it should be

int IntArray::size()

and int IntArray::&operator [] ( int i ) const

does not exist in your class definition (instead you have an = operator that you don't implement), and the '&' doesn't belong before the word 'operator'.

Good Luck!

THANK YOU THANK YOU THANK YOU!! the size() stuff and the = operator stuff were just dumb ... and i had no clue about the & part ... thanks for looking this over. i couldn't see anymore!!

crq

#include <iostream>
using namespace std;

class IntArray {
public:
  IntArray ( int sz = 10 );      
  IntArray ( const IntArray &x);
  virtual ~IntArray();
  int size () const;
  int &operator [] ( int i ) const;
private:
  int d_num_elements;
  int *d_array;
private:
  void init (const int *x, int sz);
};

IntArray::
IntArray  ( int sz )
{
  cout << "!!! default constructor called " <<endl;
  init ( NULL, sz);
}

IntArray::
IntArray ( const IntArray &x )
{
  cout<< "!!!  constructor" <<endl;
  init ( x.d_array, x.size() );
}

IntArray::
~IntArray()
{
  cout << "!!! IntArray destructor " <<endl;
  if ( size() > 0){
    delete [] d_array;
  }
}

int IntArray::
size () const
{
  return (d_num_elements);
}

int& IntArray::
operator [] ( int i ) const
{
  return d_array[i];
}

void IntArray::
init ( const int *data, int sz)
{
  d_num_elements = sz;
  if ( 0 == d_num_elements ){                  // if no elements in array
    d_array = NULL;
  }
  else{
    d_array = new int [d_num_elements];
    // allocate memory
    // if new returns NULL then no
    // more available memory
  }
  for (int i=0; i < sz; i++ ){
    // Loop Invariant:
    // for all j:0 .. i-1, self[j] == 0 OR
    //    self[j] == data[j]
    if ( NULL == data ){
      (*this)[i] = 0;
    }
    else{
      (*this)[i] = data[i];
    }
  }
}

int main () {
  IntArray a(5);
  IntArray b(8);
  cout << a.size() <<endl;
  cout << b.size() <<endl;
  return(0); 
}

Probably the most important point in the above fix is that the data members are private instead of protected. Protected data is always a bad idea, always unsafe, and always should be avoided.

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.