473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Incrementing a pointer

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
9 3371
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
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
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
Do wt_.begin() and wt_.end() return iterators? If so, that should work.
Jan 5 '07 #8
horace1
1,510 Expert 1GB
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
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

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

Similar topics

8
by: BigMan | last post by:
I wonder if the C++ standard says what should happen if I increment a pointer to a one-past-the-end value. I think it says that it is perfectly legal to increment a pointer to the last element...
13
by: John Leslie | last post by:
Hi, If I have: struct foof { long x; }; void blarg(void) {
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
3
by: Adam Sandler | last post by:
Hello, I'm able to reproduce my problem but I haven't been able to figure out why it is happening. MS does have an article about such behavior in...
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.