By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

Incrementing a pointer

P: 46
I have created a new data class which consists of four floats. I have created the copy constructor and the assignment operator= and they seem all right.

How do I define an operator++ for the pointer to the new data type?

Thanks in advance.
Dec 29 '06 #1
Share this Question
Share on Google+
9 Replies


Expert 100+
P: 1,510
I have created a new data class which consists of four floats. I have created the copy constructor and the assignment operator= and they seem all right.

How do I define an operator++ for the pointer to the new data type?

Thanks in advance.
The problem with the increment and decrement operators is that there are prefix and postfix versions, i.e. ++i, i++, --i and i--. To differentiate between the prefix and postfix versions of operator++ and operator-- the compiler adds a dummy int parameter to the postfix call. e.g. from a class which processes octal number
Expand|Select|Wrap|Line Numbers
  1.         int operator++(void)                                        // prefix ++
  2.             { 
  3.              return octal_data = (octal_data + 1) % 8;     // increment & return
  4.             }
  5.         int operator++(int)                                        // postfix ++
  6.             {                                                  
  7.              int temp = octal_data;                        // save current value
  8.              octal_data = (octal_data + 1) % 8;                     // increment
  9.              return temp;                                    // return old value
  10.             }
  11.  
Dec 30 '06 #2

P: 46
What you say is true, horace1, but your example increments data, while I need to increment and decrement a pointer to that data. Like:

Expand|Select|Wrap|Line Numbers
  1. floatData[2] = { 5.9f, 4.8f };
  2. *fltPtr = floatData;                // Pointer points to 5.9
  3. ++fltPtr;                           // increment pointer by 4 bytes
  4. cout << *flpPtr;                    // Prints 4.8
  5.  
  6.  
I need to over ride the function: ++fltPtr

Thanks
Jan 2 '07 #3

Expert 100+
P: 1,510
What you say is true, horace1, but your example increments data, while I need to increment and decrement a pointer to that data. Like:

Expand|Select|Wrap|Line Numbers
  1. floatData[2] = { 5.9f, 4.8f };
  2. *fltPtr = floatData;                // Pointer points to 5.9
  3. ++fltPtr;                           // increment pointer by 4 bytes
  4. cout << *flpPtr;                    // Prints 4.8
  5.  
  6.  
I need to over ride the function: ++fltPtr

Thanks
no need as pointer arithmetic automatically takes account of the length of an object, e.g. as with structures in C
Jan 2 '07 #4

P: 46
no need as pointer arithmetic automatically takes account of the length of an object, e.g. as with structures in C
I have a class not a structure:
Expand|Select|Wrap|Line Numbers
  1. class  WindsTableElement
  2. {
  3.  
  4.  public:
  5.   float altitude;      ///< Current altitude in feet above sea level.
  6.   float windMagnitude; ///< Wind velocity magnitude in ft/s.
  7.   float windDirection; ///< Wind velocity direction in degrees with  
  8.                         ///< respect to north.
  9.   float windUpdraft;   ///< Wind updraft in ft/s.
  10.  
  11. WindsTableElement();
  12.  
  13. WindsTableElement(const WindsTableElement &); 
  14.  
  15. const WindsTableElement &operator=(const WindsTableElement &);
  16.  
  17. };
  18.  
Do you mean if I have a pointer

WindsTableElement *wtePtr;

and I write

++wtePtr

that wtePtr will increment by 16?

If so, why does the compiler gvie me an error for not overloadng an operator++ for a pointer to WindsTableElement?
Jan 2 '07 #5

Expert 100+
P: 1,510
I have a class not a structure:
Do you mean if I have a pointer

WindsTableElement *wtePtr;

and I write

++wtePtr

that wtePtr will increment by 16?

If so, why does the compiler gvie me an error for not overloadng an operator++ for a pointer to WindsTableElement?
yes, consider the following
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class  WindsTableElement
  5. {
  6.  public:
  7.   float altitude;      ///< Current altitude in feet above sea level.
  8.   float windMagnitude; ///< Wind velocity magnitude in ft/s.
  9.   float windDirection; ///< Wind velocity direction in degrees with  
  10.                         ///< respect to north.
  11.   float windUpdraft;   ///< Wind updraft in ft/s.
  12.  
  13. WindsTableElement() {};  // ** default constructor
  14.  
  15. WindsTableElement(const WindsTableElement &); 
  16.  
  17. const WindsTableElement &operator=(const WindsTableElement &);
  18.  
  19. };
  20.  
  21. int main()
  22. {
  23.     WindsTableElement x, w[10];
  24.     WindsTableElement *ptr =w;
  25.     for (int i=0; i<5;i++)
  26.      cout << " ptr " << ptr++ << endl;  // ** this OK
  27.     //x++;                                // ** cannot do this
  28.     cin.get();
  29. }
  30.  
when run this prints

ptr 0x22fed0
ptr 0x22fee0
ptr 0x22fef0
ptr 0x22ff00
ptr 0x22ff10

on each loop the pointer is incremeneted by 0x10 (16 decimal)

if you remove the // from the front of
Expand|Select|Wrap|Line Numbers
  1.     //x++;                                // ** cannot do this
  2.  
you then get a compiler error message such as
no `operator++(int)' declared for postfix `++'

indicating you need an overloaded postfix operator++() for class WindsTableElement

no idea why you get an error, post the code ?
Jan 2 '07 #6

P: 46
Now the problem has to do with a pointer in std::copy. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <iterator> 
  3. #include <iostream>
  4. using std::cout;
  5. using std::endl;
  6. #include "WindDataReceiveBuffer.h"
  7. #include "BufferServer.h"
  8.  
  9. int main()
  10. {
  11.     BufferServer bs;
  12.  
  13.     WindDataReceiveBuffer *wd =  WindDataReceiveBuffer
  14.        ::Instance();
  15.  
  16.     const int FLT_ARY_SIZE = 12;
  17.     float flt[FLT_ARY_SIZE] = {45.6f, 34.0f, 67.5f, 56.4f, 20.0f, 
  18.         41.45f, 89.0f, 11.5f, 34.5f, 11.6f, 30.6f, 29.45f};
  19.     char byte[1000];
  20.     byte[0] = 'W';
  21.     byte[1] = 0;
  22.     byte[2] = 3;
  23.     memcpy(byte+3, flt, FLT_ARY_SIZE * 4 *sizeof(float));
  24.     std::ostream_iterator< float > OutIter(cout, " ");
  25.     std::copy(flt, flt+(FLT_ARY_SIZE*4), OutIter );
  26.     bs.setBufr(byte, *wd);
  27.  
  28.     WindTable wt_;
  29.     wt_ = wd->getWindTable();
  30.     cout << endl << "Contents of float vector: ";
  31.     std::copy(wt_.begin(), wt_.end(), OutIter);
  32.     return 0;
  33. }
The error message points to:

std::copy(wt_.begin(), wt_.end(), OutIter);
Jan 4 '07 #7

P: 38
Do wt_.begin() and wt_.end() return iterators? If so, that should work.
Jan 5 '07 #8

Expert 100+
P: 1,510
as indicated by tavianator the code looks OK as per specification of copy() see
http://www.josuttis.com/libbook/algo/copy1.cpp.html

- what is the error message ?
Jan 5 '07 #9

P: 46
as indicated by tavianator the code looks OK as per specification of copy() see
http://www.josuttis.com/libbook/algo/copy1.cpp.html

- what is the error message ?
Here is the error mesage:

d:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algobase.h:247: error: no match for 'operator=' in '(&__result)->std::ostream_iterator<_Tp, _CharT, _Traits>::operator* [with _Tp = float, _CharT = char, _Traits = std::char_traits<char>]() = *__first'
d:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stream_iterator.h:192: note: candidates are: std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = float, _CharT = char, _Traits = std::char_traits<char>]
d:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stream_iterator.h:154: note: std::ostream_iterator<float, char, std::char_traits<char> >& std::ostream_iterator<float, char, std::char_traits<char> >::operator=(const std::ostream_iterator<float, char, std::char_traits<char> >&)
Jan 8 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.