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