Incrementing a pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PaperPilot
    New Member
    • Nov 2006
    • 46

    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.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by PaperPilot
    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
    Code:
            int operator++(void)                                        // prefix ++
                { 
                 return octal_data = (octal_data + 1) % 8;     // increment & return
                }
            int operator++(int)                                        // postfix ++
                {                                                  
                 int temp = octal_data;                        // save current value
                 octal_data = (octal_data + 1) % 8;                     // increment
                 return temp;                                    // return old value
                }

    Comment

    • PaperPilot
      New Member
      • Nov 2006
      • 46

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

      Code:
      floatData[2] = { 5.9f, 4.8f };
      *fltPtr = floatData;                // Pointer points to 5.9
      ++fltPtr;                           // increment pointer by 4 bytes
      cout << *flpPtr;                    // Prints 4.8
      I need to over ride the function: ++fltPtr

      Thanks

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by PaperPilot
        What you say is true, horace1, but your example increments data, while I need to increment and decrement a pointer to that data. Like:

        Code:
        floatData[2] = { 5.9f, 4.8f };
        *fltPtr = floatData;                // Pointer points to 5.9
        ++fltPtr;                           // increment pointer by 4 bytes
        cout << *flpPtr;                    // Prints 4.8
        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

        Comment

        • PaperPilot
          New Member
          • Nov 2006
          • 46

          #5
          Originally posted by horace1
          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:
          Code:
          class  WindsTableElement
          {
          
           public:
            float altitude;      ///< Current altitude in feet above sea level.
            float windMagnitude; ///< Wind velocity magnitude in ft/s.
            float windDirection; ///< Wind velocity direction in degrees with  
                                  ///< respect to north.
            float windUpdraft;   ///< Wind updraft in ft/s.
          
          WindsTableElement();
          
          WindsTableElement(const WindsTableElement &); 
          
          const WindsTableElement &operator=(const WindsTableElement &);
          
          };
          Do you mean if I have a pointer

          WindsTableEleme nt *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 WindsTableEleme nt?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by PaperPilot
            I have a class not a structure:
            Do you mean if I have a pointer

            WindsTableEleme nt *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 WindsTableEleme nt?
            yes, consider the following
            Code:
            #include <iostream>
            using namespace std;
            
            class  WindsTableElement
            {
             public:
              float altitude;      ///< Current altitude in feet above sea level.
              float windMagnitude; ///< Wind velocity magnitude in ft/s.
              float windDirection; ///< Wind velocity direction in degrees with  
                                    ///< respect to north.
              float windUpdraft;   ///< Wind updraft in ft/s.
            
            WindsTableElement() {};  // ** default constructor
            
            WindsTableElement(const WindsTableElement &); 
            
            const WindsTableElement &operator=(const WindsTableElement &);
            
            };
            
            int main()
            {
                WindsTableElement x, w[10];
                WindsTableElement *ptr =w;
                for (int i=0; i<5;i++)
                 cout << " ptr " << ptr++ << endl;  // ** this OK
                //x++;                                // ** cannot do this
                cin.get();
            }
            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
            Code:
                //x++;                                // ** cannot do this
            you then get a compiler error message such as
            no `operator++(int )' declared for postfix `++'

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

            no idea why you get an error, post the code ?

            Comment

            • PaperPilot
              New Member
              • Nov 2006
              • 46

              #7
              Now the problem has to do with a pointer in std::copy. Here is my code:
              Code:
              #include <vector>
              #include <iterator> 
              #include <iostream>
              using std::cout;
              using std::endl;
              #include "WindDataReceiveBuffer.h"
              #include "BufferServer.h"
              
              int main()
              {
                  BufferServer bs;
              
                  WindDataReceiveBuffer *wd =  WindDataReceiveBuffer
                     ::Instance();
              
                  const int FLT_ARY_SIZE = 12;
                  float flt[FLT_ARY_SIZE] = {45.6f, 34.0f, 67.5f, 56.4f, 20.0f, 
                      41.45f, 89.0f, 11.5f, 34.5f, 11.6f, 30.6f, 29.45f};
                  char byte[1000];
                  byte[0] = 'W';
                  byte[1] = 0;
                  byte[2] = 3;
                  memcpy(byte+3, flt, FLT_ARY_SIZE * 4 *sizeof(float));
                  std::ostream_iterator< float > OutIter(cout, " ");
                  std::copy(flt, flt+(FLT_ARY_SIZE*4), OutIter );
                  bs.setBufr(byte, *wd);
              
                  WindTable wt_;
                  wt_ = wd->getWindTable();
                  cout << endl << "Contents of float vector: ";
                  std::copy(wt_.begin(), wt_.end(), OutIter);
                  return 0;
              }
              The error message points to:

              std::copy(wt_.b egin(), wt_.end(), OutIter);

              Comment

              • tavianator
                New Member
                • Dec 2006
                • 38

                #8
                Do wt_.begin() and wt_.end() return iterators? If so, that should work.

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  as indicated by tavianator the code looks OK as per specification of copy() see
                  http://www.josuttis.co m/libbook/algo/copy1.cpp.html

                  - what is the error message ?

                  Comment

                  • PaperPilot
                    New Member
                    • Nov 2006
                    • 46

                    #10
                    Incrementing a pointer

                    Originally posted by horace1
                    as indicated by tavianator the code looks OK as per specification of copy() see
                    Nicolai Josuttis, The C++ Standard Library - A Tutorial and Reference, copy() algorithm


                    - 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_i terator<_Tp, _CharT, _Traits>::opera tor* [with _Tp = float, _CharT = char, _Traits = std::char_trait s<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_it erator<_Tp, _CharT, _Traits>& std::ostream_it erator<_Tp, _CharT, _Traits>::opera tor=(const _Tp&) [with _Tp = float, _CharT = char, _Traits = std::char_trait s<char>]
                    d:/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stream_iterator .h:154: note: std::ostream_it erator<float, char, std::char_trait s<char> >& std::ostream_it erator<float, char, std::char_trait s<char> >::operator=(co nst std::ostream_it erator<float, char, std::char_trait s<char> >&)
                    Last edited by PaperPilot; Jan 8 '07, 06:21 PM. Reason: Need to add error message

                    Comment

                    Working...