944,131 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2608
  • C++ RSS
Jan 23rd, 2005
0

pointer/reference ? issues ... need help with simple code

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include "/unc/hedlund/121/lib/cpluslib.h"
  5.  
  6. class IntArray{
  7.  
  8.  
  9. public:
  10. IntArray ( int sz = 10 );
  11.  
  12. IntArray ( const IntArray &x);
  13.  
  14. virtual ~IntArray();
  15.  
  16. int size () const;
  17.  
  18. int &operator = ( int i ) const;
  19.  
  20. protected:
  21. int d_num_elements;
  22. int *d_array;
  23.  
  24.  
  25. private:
  26. void init (const int *x, int sz);
  27. };
  28.  
  29. IntArray::
  30. IntArray ( int sz )
  31. {
  32. cout << "!!! default constructor called " <<endl;
  33. init ( NULL, sz);
  34. }
  35.  
  36. IntArray::
  37. IntArray ( const IntArray &x )
  38. {
  39. cout<< "!!! constructor" <<endl;
  40. init ( x.d_array, x.size() );
  41. }
  42.  
  43. IntArray::
  44. ~IntArray()
  45. {
  46. cout << "!!! IntArray destructor " <<endl;
  47. if ( size() > 0){
  48. delete [] d_array;
  49. }
  50. }
  51.  
  52. int
  53. size ()
  54. {
  55. return (d_num_elements);
  56. }
  57.  
  58. int IntArray::
  59. &operator [] ( int i ) const
  60. {
  61. return d_array[i];
  62. }
  63.  
  64. void IntArray::
  65. init ( const int *data, int sz)
  66. {
  67. d_num_elements = sz;
  68. if ( 0 == d_num_elements ){ // if no elements in array
  69. d_array = NULL;
  70. }
  71. else{
  72. d_array = new int [d_num_elements];
  73. // allocate memory
  74. // if new returns NULL then no
  75. // more available memory
  76. }
  77. for (int i=0; i < sz; i++ ){
  78. // Loop Invariant:
  79. // for all j:0 .. i-1, self[j] == 0 OR
  80. // self[j] == data[j]
  81. if ( NULL == data ){
  82. self [i] = 0;
  83. }
  84. else{
  85. self[i] = data[i];
  86. }
  87. }
  88. }
  89.  
  90.  
  91. int main () {
  92. IntArray a(5);
  93. IntArray b(8);
  94. cout << a.size() <<endl;
  95. cout << b.size() <<endl;
  96. return(0);
  97. }
  98. [\code]

thanks so much for looking at this!!

crq
Similar Threads
crq
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crq is offline Offline
24 posts
since Jan 2005
Jan 23rd, 2005
0

Re: pointer/reference ? issues ... need help with simple code

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!
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jan 23rd, 2005
0

Re: pointer/reference ? issues ... need help with simple code

Quote 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!
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
crq
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crq is offline Offline
24 posts
since Jan 2005
Jan 23rd, 2005
0

Re: pointer/reference ? issues ... need help with simple code

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class IntArray {
  5. public:
  6. IntArray ( int sz = 10 );
  7. IntArray ( const IntArray &x);
  8. virtual ~IntArray();
  9. int size () const;
  10. int &operator [] ( int i ) const;
  11. private:
  12. int d_num_elements;
  13. int *d_array;
  14. private:
  15. void init (const int *x, int sz);
  16. };
  17.  
  18. IntArray::
  19. IntArray ( int sz )
  20. {
  21. cout << "!!! default constructor called " <<endl;
  22. init ( NULL, sz);
  23. }
  24.  
  25. IntArray::
  26. IntArray ( const IntArray &x )
  27. {
  28. cout<< "!!! constructor" <<endl;
  29. init ( x.d_array, x.size() );
  30. }
  31.  
  32. IntArray::
  33. ~IntArray()
  34. {
  35. cout << "!!! IntArray destructor " <<endl;
  36. if ( size() > 0){
  37. delete [] d_array;
  38. }
  39. }
  40.  
  41. int IntArray::
  42. size () const
  43. {
  44. return (d_num_elements);
  45. }
  46.  
  47. int& IntArray::
  48. operator [] ( int i ) const
  49. {
  50. return d_array[i];
  51. }
  52.  
  53. void IntArray::
  54. init ( const int *data, int sz)
  55. {
  56. d_num_elements = sz;
  57. if ( 0 == d_num_elements ){ // if no elements in array
  58. d_array = NULL;
  59. }
  60. else{
  61. d_array = new int [d_num_elements];
  62. // allocate memory
  63. // if new returns NULL then no
  64. // more available memory
  65. }
  66. for (int i=0; i < sz; i++ ){
  67. // Loop Invariant:
  68. // for all j:0 .. i-1, self[j] == 0 OR
  69. // self[j] == data[j]
  70. if ( NULL == data ){
  71. (*this)[i] = 0;
  72. }
  73. else{
  74. (*this)[i] = data[i];
  75. }
  76. }
  77. }
  78.  
  79. int main () {
  80. IntArray a(5);
  81. IntArray b(8);
  82. cout << a.size() <<endl;
  83. cout << b.size() <<endl;
  84. return(0);
  85. }
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: extracting the middle number of 3 inputs
Next Thread in C++ Forum Timeline: assert question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC