| | |
pointer/reference ? issues ... need help with simple code
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 24
Reputation:
Solved Threads: 0
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
thanks so much for looking at this!!
crq
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
C++ Syntax (Toggle Plain Text)
#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
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!
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!
•
•
Join Date: Jan 2005
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Chainsaw
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!
crq
C++ Syntax (Toggle Plain Text)
#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); }
I'm here to prove you wrong.
![]() |
Similar Threads
- Simple code help (Python)
- char pointer and array issues (C++)
- Issues with 3 compilers and..code (C++)
- simple code (C++)
- CAn anyone provide me a very simple code for shopping cart? (PHP)
- pointer to a refernce (C++)
Other Threads in the C++ Forum
- Previous Thread: extracting the middle number of 3 inputs
- Next Thread: assert question
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






