473,770 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

user-defined iterator

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<SixBytesm_ 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_bac k(SixBytes());
m_list.push_bac k(SixBytes());}
};

int main(){
char data[13];
data[0] = 2;
MyCont m;
std::copy(m.beg in(), 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_ba se_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 `_OutputIterato r
std::__copy_ni2 (_InputIterator , _InputIterator, _OutputIterator ,
__false_type) [with _InputIterator = MyCont::Iterato r, _OutputIterator
= char*]'
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h: 327: instantiated from `_OutputIterato r
std::__copy_ni1 (_InputIterator , _InputIterator, _OutputIterator ,
__false_type) [with _InputIterator = MyCont::Iterato r, _OutputIterator
= char*]'
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_algobase.h: 358: instantiated from `_OutputIterato r
std::copy(_Inpu tIterator, _InputIterator, _OutputIterator ) [with
_InputIterator = MyCont::Iterato r, _OutputIterator = char*]'
container.cpp:4 1: instantiated from here
/usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/../../../../include/c++/
3.4.1/bits/stl_iterator_ba se_types.h:129: error: no type named
`iterator_categ ory' in `class MyCont::Iterato r'

....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_catego y?
What about the simple char* parameters given to the std::copy
algorithm ? why are they ok?

thank you all,
vasilis.

Jun 15 '07 #1
3 4515
vasili wrote:
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.
Since you want to use standard algorithm, it _may_ require that you
specialize 'iterator_trait s' for your custom iterator.
>
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<SixBytesm_ 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_bac k(SixBytes());
m_list.push_bac k(SixBytes());}
};

int main(){
char data[13];
data[0] = 2;
MyCont m;
std::copy(m.beg in(), 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_ba se_types.h: In instantiation of
`std::iterator_ traits<MyCont:: Iterator>':
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^
That's the implicit specialisation the compiler attempts. And fails.
[..]

Do i have to provide my implementation with these types?
Yes, if you want your code to compile.
If yes, what about iterator_catego y?
Yes
What about the simple char* parameters given to the std::copy
algorithm ? why are they ok?
Yes, because the standard library most likely already contains the
specialisation of 'itetator_trait s' for built-in pointer types.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 15 '07 #2
On Fri, 15 Jun 2007 07:18:55 -0700, vasili wrote:
>I have a simple issue.
If it were simple it wouldn't be C++.
>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.
AFAICS, you want to iterate over the list _and_ the 'six position
array' with a new iterator (not just re-use the
list<SixBytes>: :iterator). In this case you need to define your own
iterator with all required typedefs and functions. Look for 'custom
STL iterator', e.g.
http://www.stanford.edu/class/cs107l...-Iterators.pdf
http://www.oonumerics.org/tmpw00/becker.html
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 15 '07 #3
On Jun 15, 5:02 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
vasili wrote:
I have a simple issue.
:-)
It takes a complicated language to solve complicated
problems:-).
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.
Since you want to use standard algorithm, it _may_ require that you
specialize 'iterator_trait s' for your custom iterator.
He must do something to ensure that iterator_traits <Iterator>
contains the proper typedefs. The generic implementation of
this template supposes that there are corresponding typedef's in
the Iterator class; the standard library also contains a partial
specialization for pointers (since pointers obviously don't
contain the necessary typedef's). He can thus either provide a
custom specialization, with the necessary typedef's, or put the
typedef's in his class. The latter is the classical solution,
and the standard offers a class template, std::iterator, to help
here. All he has to do is have his iterator derive (publicly)
from the appropriate instantiation of std::iterator, and it
should suffice.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

60
7307
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm The detector requires javascript 1.0 to work. This translates to netscape 2.0 and IE 3.0 (although maybe IE 2.0 also works with it)
3
4142
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability. The User Experience, or how the user experiences the end product, is the key to acceptance. And that is where User Interface Design enters the design process. While product engineers focus on the technology, usability specialists focus on the user...
6
11300
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" %> The web user control contains the following server controls
1
7587
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
7
2918
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
0
3937
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something like this is a bug or that .NET doesn't support what I trying to do. I hope that one that is is microsoft certified read this because this must be a bug.
2
4823
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be required: <%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %> Assuming that the ASPX page doesn't use a code-behind, I can access the properties, events etc. of the user control in the ASPX page in this way (assume that the ASPX page...
1
1974
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users are created but the account is disabled (see the flag User.AccountDisabled = False ). There is also another problem even if the user does not exist , the application returns to me with the message that the user already exist. Thank you for...
0
3234
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have given all the attributes which are needed, for the user, in the code and also the proper path where the user has to be added. Please have a look at my code CODE] // This is a class file which stores all the info required for the user
9
6281
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who only has read access to the users table so that users can log in. Once a user has been logged in successfully I want to escalate that user's access level to one appropriate to their role, which will include switching the postgres user they are...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.