circular buffer und iterator



  • hallo zusammen,

    ich habe mir meinen eigenen circular buffer auf basis von boost gebastelt, aber irgendwo stimmt da wohl was nicht. ich gebe zu, es sind meine ersten versuche auf dem gebiet der iteratoren :).

    Hier mein Testcode:

    CircularBuffer<int> test(10);
                test.add(1);
                test.add(2);
                test.add(3);
    
                CircularBuffer<int>::iterator_type it = test.begin();
                while(it != test.end()){
                    std::cout<<it<<endl;
                    it++;
                }
    

    Mein Buffer sieht gekürzt so aus:

    #pragma once
    
    #ifndef CIRCULARBUFFER_H
    #define	CIRCULARBUFFER_H
    
    #include <boost/circular_buffer.hpp>
    
    namespace A {
        namespace B {
    
            template <typename T>
            class CircularBuffer {
            public:
    
                CircularBuffer(size_t size);
                CircularBuffer(const CircularBuffer & orig);
                virtual ~CircularBuffer();
    
                void add(const T & t);
    
            protected:
    
                typedef boost::circular_buffer<T> CBuffer;
                CBuffer buffer;
    
            public:
    
                template <typename PointerType>
                class iterator : public CBuffer::iterator {
                };
    
                typedef iterator<CBuffer*> iterator_type;
                typedef iterator<const CBuffer*> const_iterator_type;
    
                iterator_type begin();
                iterator_type end();
                const_iterator_type begin() const;
                const_iterator_type end() const;
    
            };
    
            template <typename T>
            std::ostream operator<<(std::ostream & s, typename CircularBuffer<T>::iterator_type it){
                return s<<*it;
            }
    
            template <typename T>
            CircularBuffer<T>::CircularBuffer(size_t size) : buffer(size) {
            }
    
            template <typename T>
            CircularBuffer<T>::CircularBuffer(const CircularBuffer & orig) : buffer(orig.buffer) {
            }
    
            template <typename T>
            CircularBuffer<T>::~CircularBuffer() {
            }
    
            template <typename T>
            void
            CircularBuffer<T>::add(const T & t) {
                buffer.push_back(t);
            }
    
            template<typename T>
            typename CircularBuffer<T>::iterator_type
            CircularBuffer<T>::begin() {
                return buffer.begin();
            }
    
            template<typename T>
            typename CircularBuffer<T>::const_iterator_type
            CircularBuffer<T>::begin() const {
                return buffer.begin();
            }
    
            template<typename T>
            typename CircularBuffer<T>::iterator_type
            CircularBuffer<T>::end() {
                return buffer.end();
            }
    
            template<typename T>
            typename CircularBuffer<T>::const_iterator_type
            CircularBuffer<T>::end() const {
                return buffer.end();
            }
        }
    }
    
    #endif	/* CIRCULARBUFFER_H */
    

    Leider bekomme ich folgende Fehlermeldung, welche mir so richtig nix sagt:

    In member function ‘typename A::B::CircularBuffer<T>::iterator_type A::B::CircularBuffer<T>::begin() [with T = int]’:
    A.cc:238:   instantiated from here
    CircularBuffer.h:307: error: conversion from ‘boost::cb_details::iterator<boost::circular_buffer<int, std::allocator<int> >, boost::cb_details::nonconst_traits<std::allocator<int> > >’ to non-scalar type ‘A::B::CircularBuffer<int>::iterator<boost::circular_buffer<int, std::allocator<int> >*>’ requested
    CircularBuffer.h: In member function ‘typename A::B::CircularBuffer<T>::iterator_type A::B::CircularBuffer<T>::end() [with T = int]’:
    A.cc:239:   instantiated from here
    CircularBuffer.h:331: error: conversion from ‘boost::cb_details::iterator<boost::circular_buffer<int, std::allocator<int> >, boost::cb_details::nonconst_traits<std::allocator<int> > >’ to non-scalar type ‘A::B::CircularBuffer<int>::iterator<boost::circular_buffer<int, std::allocator<int> >*>’ requested
    

    Kann mir jemand weiterhelfen???

    Danke!



  • Warum dieser Mummenschanz mit den Iteratoren?

    sven_ schrieb:

    class CircularBuffer {
      //....
      public:
    
      template <typename PointerType>
      class iterator : public CBuffer::iterator {
      };
    
      typedef iterator<CBuffer*> iterator_type;
      typedef iterator<const CBuffer*> const_iterator_type;
      //...
    }
    

    Anstatt das näherliegende

    typedef typename CBuffer::iterator iterator_type;
      typedef typename CBuffer::const_iterator const_iterator_type;
    

    ?

    PS: ist jemand der Moderatoren so nett, die Fehlermeldung umzubrechen?



  • hallo,

    erst mal vielen dank für die hilfe. also das mit den iteratoren habe ich wie gesagt so gemacht, weil ich es nicht anders wußte. nun hab ich die klasse gegen deine types ersetzt, und ich komme weiter. allerdings bricht er ab, wenn ich ein element per erase aus den buffer nehmen will. mein code dazu ist nur:

    template<typename T>
            typename CircularBuffer<T>::iterator_type
            CircularBuffer<T>::erase(iterator_type pos){
                buffer.erase(pos);
            }
    

    Konkret kommt ein SegFault aus den internen Boost Methoden (debug_iterator_base::next()). Was mach ich falsch???

    Schon mal danke.



  • sven_ schrieb:

    template<typename T>
            typename CircularBuffer<T>::iterator_type
            CircularBuffer<T>::erase(iterator_type pos){
                buffer.erase(pos);
            }
    

    Konkret kommt ein SegFault aus den internen Boost Methoden (debug_iterator_base::next()). Was mach ich falsch???

    Schon mal danke.

    Sollte die Methode nicht was zurückgeben?

    Im Compiler auf jeden Fall mehr Warnungen aktivieren... 🙂



  • hallo nochmal,

    ich merke gerade, es liegt gar nicht am earse, sondern am 2. aufruf von it.begin(). also ist doch wohl irgendwie noch mein iterator falsch. mh. 😞



  • hallo nochmal,

    stimmt! Da fehlte was. Super. Sauber neubauen und drüber schauen hilft vielleicht. Ich danke dir. 🙂

    Vielen Dank.


Anmelden zum Antworten