How to define function which operates on iterator convertible toT*

Discussion in 'C++' started by AlesD, Aug 8, 2004.

  1. AlesD

    AlesD Guest

    Hello,

    maybe my question is stupid, but...

    I have function/algorithm which operates on sequence holding objects of
    type T. The problem is that I do not know (at the declaration time)
    which sequence (vector<T> or list<T>) will be used.

    class element_t {
    void some_method(void);
    }

    template <class InputIterator>
    void visitor(InputIterator first, InputIterator last)
    {
    // some code
    first->some_method(); // Error here
    }

    How can I specify that InputIterator is convertible to T*?

    Thanks in advance, Ales
     
    AlesD, Aug 8, 2004
    #1
    1. Advertising

  2. AlesD

    David Hilsee Guest

    Re: How to define function which operates on iterator convertible to T*

    "AlesD" <> wrote in message
    news:cf5vkg$2vc$...
    > Hello,
    >
    > maybe my question is stupid, but...
    >
    > I have function/algorithm which operates on sequence holding objects of
    > type T. The problem is that I do not know (at the declaration time)
    > which sequence (vector<T> or list<T>) will be used.
    >
    > class element_t {
    > void some_method(void);
    > }
    >
    > template <class InputIterator>
    > void visitor(InputIterator first, InputIterator last)
    > {
    > // some code
    > first->some_method(); // Error here
    > }
    >
    > How can I specify that InputIterator is convertible to T*?


    What's the error? The following works just fine:

    #include <list>

    struct element_t {
    void some_method(){}
    };

    template <class It>
    void visitor(It beg, It end) {
    beg->some_method();
    }

    int main() {
    std::list<element_t> elems(1);
    visitor( elems.begin(), elems.end() );
    }

    If the container holds pointers instead of instances of element_t, then you
    should dereference the iterator before using operator->
    ((*beg)->some_method() instead of beg->some_method()).

    --
    David Hilsee
     
    David Hilsee, Aug 8, 2004
    #2
    1. Advertising

  3. OSstream querry

    Hi,

    I am using ostrstream object.I am using the str() function to get the char
    buffer. The function is returning the pointer of char buffer , but it is
    adding some garbage at the end. This behaviour is not predictable .
    Sometimes it is returning the char buffer correctly , sometimes it is
    returning the char buffer and some garbage attached to it at the end.I am
    initialising the ostrstream object by fill('\0') function.

    Can U please suggest some solution.

    I am writing the code below for reference.

    tBool clClusterCheckResults::logUplinkZeroCellError(const clCluster&
    corfCluster,
    RBINTValOrderedVector <int> a_vZCsNotRetained)
    {
    bool a_bReturnValue(false);
    ostrstream myBuffer;
    myBuffer.fill('\0');
    a_bReturnValue = true;

    myBuffer << corfCluster // << opearor is overloaded for clCluster class
    << " WARNING: The Cluster has no Uplink ZeroCells. "
    << " The following Zero Cells with Cobounding OC, with Uplinks are not
    retained "
    << " in the higher level. (ZC Index) - ";

    for(int iZeroCellIterator = 0 ;
    iZeroCellIterator < a_vZCsNotRetained.length() ;
    iZeroCellIterator++)
    {
    myBuffer << a_vZCsNotRetained[iZeroCellIterator] << " ";
    }

    myBuffer << "END\n";
    cout<< "``````````PRINTING mybuffer logUplinkZeroCellError`````````\n";
    cout<< myBuffer.str(); //Here the output is coming errorous
    cout<< "``````````OVER`````````````````````````````````````````````\n";

    m_oReport.logErrorMessage(myBuffer);

    m_oResultsCounter.incrementErrorCount(
    corfCluster.getLevel(),
    UplinkZeroCellCheck,


    "David Hilsee" <> wrote in message
    news:p...
    > "AlesD" <> wrote in message
    > news:cf5vkg$2vc$...
    > > Hello,
    > >
    > > maybe my question is stupid, but...
    > >
    > > I have function/algorithm which operates on sequence holding objects of
    > > type T. The problem is that I do not know (at the declaration time)
    > > which sequence (vector<T> or list<T>) will be used.
    > >
    > > class element_t {
    > > void some_method(void);
    > > }
    > >
    > > template <class InputIterator>
    > > void visitor(InputIterator first, InputIterator last)
    > > {
    > > // some code
    > > first->some_method(); // Error here
    > > }
    > >
    > > How can I specify that InputIterator is convertible to T*?

    >
    > What's the error? The following works just fine:
    >
    > #include <list>
    >
    > struct element_t {
    > void some_method(){}
    > };
    >
    > template <class It>
    > void visitor(It beg, It end) {
    > beg->some_method();
    > }
    >
    > int main() {
    > std::list<element_t> elems(1);
    > visitor( elems.begin(), elems.end() );
    > }
    >
    > If the container holds pointers instead of instances of element_t, then

    you
    > should dereference the iterator before using operator->
    > ((*beg)->some_method() instead of beg->some_method()).
    >
    > --
    > David Hilsee
    >
    >
     
    News For Mohan, Aug 10, 2004
    #3
  4. Re: OSstream querry

    News For Mohan wrote:
    > Hi,


    Let me ask you right away, why did you reply to a completely unrelated
    post instead of posting a new thread?

    >
    > I am using ostrstream object.I am using the str() function to get the char
    > buffer. The function is returning the pointer of char buffer , but it is
    > adding some garbage at the end. This behaviour is not predictable .
    > Sometimes it is returning the char buffer correctly , sometimes it is
    > returning the char buffer and some garbage attached to it at the end.I am
    > initialising the ostrstream object by fill('\0') function.


    There is no need to "initialise" an ostringstream object in any way.
    The constructor for the ostringstream class takes care of that.

    >
    > Can U please suggest some solution.


    Drop the call to 'fill' for starters and RTFM about ostringstream class
    to learn what 'fill' does to it.

    >
    > I am writing the code below for reference.
    >
    > tBool clClusterCheckResults::logUplinkZeroCellError(const clCluster&
    > corfCluster,
    > RBINTValOrderedVector <int> a_vZCsNotRetained)
    > {
    > bool a_bReturnValue(false);
    > ostrstream myBuffer;
    > myBuffer.fill('\0');
    > a_bReturnValue = true;
    >
    > myBuffer << corfCluster // << opearor is overloaded for clCluster class
    > << " WARNING: The Cluster has no Uplink ZeroCells. "
    > << " The following Zero Cells with Cobounding OC, with Uplinks are not
    > retained "
    > << " in the higher level. (ZC Index) - ";
    >
    > for(int iZeroCellIterator = 0 ;
    > iZeroCellIterator < a_vZCsNotRetained.length() ;
    > iZeroCellIterator++)
    > {
    > myBuffer << a_vZCsNotRetained[iZeroCellIterator] << " ";
    > }
    >
    > myBuffer << "END\n";
    > cout<< "``````````PRINTING mybuffer logUplinkZeroCellError`````````\n";
    > cout<< myBuffer.str(); //Here the output is coming errorous
    > cout<< "``````````OVER`````````````````````````````````````````````\n";
    >
    > m_oReport.logErrorMessage(myBuffer);
    >
    > m_oResultsCounter.incrementErrorCount(
    > corfCluster.getLevel(),
    > UplinkZeroCellCheck,
    >
    > [...]
     
    Victor Bazarov, Aug 10, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max

    How DOMError operates?

    Max, Oct 17, 2006, in forum: XML
    Replies:
    0
    Views:
    383
  2. ton
    Replies:
    3
    Views:
    986
    Mr. Arnold
    Feb 4, 2010
  3. Replies:
    10
    Views:
    159
  4. Ted Byers
    Replies:
    0
    Views:
    594
    Ted Byers
    Dec 21, 2011
  5. Greg Guarino
    Replies:
    6
    Views:
    245
Loading...

Share This Page