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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

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

 
0
  #1
Jan 23rd, 2005
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

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

 
0
  #2
Jan 23rd, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

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

 
0
  #3
Jan 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Jan 23rd, 2005
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC