V
vasili
hello All,
I have a simple issue.
I defined a custom container, that encloses a std::list, which in turn
holds objects that are a simple abstraction of a six position array.
Now, i would like to serialize the whole newly-defined container, in
order to copy the contents to another array. So i thought to define an
iterator which represented a "pointer" to the container's data. But,
when i feed these iterators to std::copy the compiler complains about
a lot of types which are defined when a std::iterator is instanced.
the code:
//i leave all unnecessary stuff out just to be clear
#include <list>
using namespace std;
class SixBytes{
public: //i don't trash the example with any accessor methods
char m_data[6];
};
class MyCont{
list<SixBytes> m_list;
public:
class Iterator{
const MyCont& m_cont;
int m_index;
public:
Iterator(const MyCont& cnt, int index=0):m_cont(cnt),
m_index(index){}
Iterator operator++(int){//postfix? just placed this
and the following methods to be "complete" //
w.r.t. the requirements of the std::copy algorithm and to this example
Iterator ret(*this);
m_index++;
return ret;
}
Iterator& operator++(){//prefix?
m_index++;
return *this;
}
char operator*(){
//...return the byte that corresponds to the
position m_
index
}
};
inline Iterator begin() const{ return Iterator(*this); }//the
start of the serialization
inline const Iterator end() const { return Iterator(*this,
m_list.size()*6); }//it's end
MyCont(){ m_list.push_back(SixBytes());
m_list.push_back(SixBytes());}
};
int main(){
char data[13];
data[0] = 2;
MyCont m;
std::copy(m.begin(), m.end(), data+1);
}//example ends here
giving this to the compiler i get this:
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_iterator_base_types.h: In instantiation of
`std::iterator_traits<MyCont::Iterator>':
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h:305: instantiated from `_OutputIterator
std::__copy_ni2(_InputIterator, _InputIterator, _OutputIterator,
__false_type) [with _InputIterator = MyCont::Iterator, _OutputIterator
= char*]'
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h:327: instantiated from `_OutputIterator
std::__copy_ni1(_InputIterator, _InputIterator, _OutputIterator,
__false_type) [with _InputIterator = MyCont::Iterator, _OutputIterator
= char*]'
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h:358: instantiated from `_OutputIterator
std::copy(_InputIterator, _InputIterator, _OutputIterator) [with
_InputIterator = MyCont::Iterator, _OutputIterator = char*]'
container.cpp:41: instantiated from here
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_iterator_base_types.h:129: error: no type named
`iterator_category' in `class MyCont::Iterator'
....more ot those errors about value_type and all such things.
Do i have to provide my implementation with these types?
If yes, what about iterator_categoy?
What about the simple char* parameters given to the std::copy
algorithm ? why are they ok?
thank you all,
vasilis.
I have a simple issue.
I defined a custom container, that encloses a std::list, which in turn
holds objects that are a simple abstraction of a six position array.
Now, i would like to serialize the whole newly-defined container, in
order to copy the contents to another array. So i thought to define an
iterator which represented a "pointer" to the container's data. But,
when i feed these iterators to std::copy the compiler complains about
a lot of types which are defined when a std::iterator is instanced.
the code:
//i leave all unnecessary stuff out just to be clear
#include <list>
using namespace std;
class SixBytes{
public: //i don't trash the example with any accessor methods
char m_data[6];
};
class MyCont{
list<SixBytes> m_list;
public:
class Iterator{
const MyCont& m_cont;
int m_index;
public:
Iterator(const MyCont& cnt, int index=0):m_cont(cnt),
m_index(index){}
Iterator operator++(int){//postfix? just placed this
and the following methods to be "complete" //
w.r.t. the requirements of the std::copy algorithm and to this example
Iterator ret(*this);
m_index++;
return ret;
}
Iterator& operator++(){//prefix?
m_index++;
return *this;
}
char operator*(){
//...return the byte that corresponds to the
position m_
index
}
};
inline Iterator begin() const{ return Iterator(*this); }//the
start of the serialization
inline const Iterator end() const { return Iterator(*this,
m_list.size()*6); }//it's end
MyCont(){ m_list.push_back(SixBytes());
m_list.push_back(SixBytes());}
};
int main(){
char data[13];
data[0] = 2;
MyCont m;
std::copy(m.begin(), m.end(), data+1);
}//example ends here
giving this to the compiler i get this:
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_iterator_base_types.h: In instantiation of
`std::iterator_traits<MyCont::Iterator>':
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h:305: instantiated from `_OutputIterator
std::__copy_ni2(_InputIterator, _InputIterator, _OutputIterator,
__false_type) [with _InputIterator = MyCont::Iterator, _OutputIterator
= char*]'
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h:327: instantiated from `_OutputIterator
std::__copy_ni1(_InputIterator, _InputIterator, _OutputIterator,
__false_type) [with _InputIterator = MyCont::Iterator, _OutputIterator
= char*]'
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h:358: instantiated from `_OutputIterator
std::copy(_InputIterator, _InputIterator, _OutputIterator) [with
_InputIterator = MyCont::Iterator, _OutputIterator = char*]'
container.cpp:41: instantiated from here
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_iterator_base_types.h:129: error: no type named
`iterator_category' in `class MyCont::Iterator'
....more ot those errors about value_type and all such things.
Do i have to provide my implementation with these types?
If yes, what about iterator_categoy?
What about the simple char* parameters given to the std::copy
algorithm ? why are they ok?
thank you all,
vasilis.