iterator help

R

Rex_chaos

Hi all,
I have designed an iterator and a container like these

template <class T>
class Iter
{
...
public:
Iter( Container& c, int c ) :cont(c), current(c)
T& operator*() {return cont[current];}
Iter& operator++() {...; return *this;}
Iter& operator--() {...; return *this;}
...
};

template <class T>
class Container
{
public:
Container( T *a, int n ) :array(a), num(n) {...}

////////////////////////////////////////////////
Iter begin() {...};
Iter end() {...};
};


/* ===============================
Here is a program for testing.
================================= */
double a[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Container<double> cont(a, 10);
Iter<double> b=cont.begin(), e=cont.end(), b2=cont.begin();

// When I take std::transform
transform( b, e, b, bind2nd(plus<double>(), 2.0) );
// it's ok in compilation time. However, some error occurs in runtime
:
// An access violation (Segmentation Fault) raised in your program.


Take a look at the source code of std::transform

template<typename _InputIter, typename _OutputIter, typename
_UnaryOperation>
_OutputIter transform(_InputIter __first, _InputIter __last,
_OutputIter __result, _UnaryOperation
__unary_op)
{
// concept requirements
__glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
__glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
// "the type returned by a _UnaryOperation"
__typeof__(__unary_op(*__first))>)

for ( ; __first != __last; ++__first, ++__result)
*__result = __unary_op(*__first);
return __result;
}

For testing, I have written a piece of code to act as the transform

for ( ;bmit != emit; ++bmit, ++bmit2) *bmit2 = *bmit + 2.0;

The code above without any problem even in runtime. It's very weird. I
even replace the source code of std::transform with the above loop,
but no help. What's going on? Please help.
 
V

Victor Bazarov

Rex_chaos said:
Hi all,
I have designed an iterator and a container like these
[...]

Post the _complete_ code (that means no "..." in implementations).

Victor
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top