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<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.

0
6/15/2007 2:18:55 PM
comp.lang.c++ 48803 articles. 0 followers. Post Follow

3 Replies
238 Views

Similar Articles

[PageSpeed] 38
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_traits' 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<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>':
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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_categoy?

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_traits' 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 


0
v.Abazarov (13255)
6/15/2007 3:02:29 PM
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/handouts/02-Custom-Iterators.pdf
http://www.oonumerics.org/tmpw00/becker.html


-- 
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
0
rpbg123 (870)
6/15/2007 3:15:35 PM
On Jun 15, 5:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> 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_traits' 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: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34

0
james.kanze (9769)
6/16/2007 9:18:25 AM
Reply:
Similar Artilces:

[ace-users] Virus found! Fw: goldfish
Hello, The message you sent to the <voyance@voyance-astro.com> is infected and has not been delivered. Please check attached files and try again --- Antivirus service provided by DrWeb Daemon (http://www.drweb.ru) ...

Multiple user profiles generation
My Operating system is Win 2000 professional connected to a network. I have created a user called 'computer' with user rights assigned to it. Whenever i login into the computer as that user, a folder in the documents and settings is created with the name as 'computer.01', 'computer.02' and so on for each login. But in the users only 'computer' is displayed. So how to rectify this problem of multiple user generation. Even when i delete that folders it again starts from 'computer.01' and so on at the next login. Please help me with this. each comp...

defining the size_t type
hi, i have seen an interesting thing: #if sizeof((char*)0 - (char*)0) == sizeof(unsigned int) typedef unsigned int size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long) typedef unsigned long size_t; #elif sizeof((char*)0 - (char*)0) == sizeof(unsigned long long) typedef unsigned long long size_t; #endif is this way of defining the size_t portable? cheers, lubos lubomir dobsik wrote: > hi, i have seen an interesting thing: > > #if sizeof((char*)0 - (char*)0) == sizeof(unsigned int) > typedef unsigned int size_t; > #elif sizeof((char*)0 - (char*)0) == size...

Pica user vs. Inch user
Picas user vs. Inches user I always thought people who use picas have more experience than people who use inches in the printing field, why does some pica user just doesn't understand the proper way to build your electronic layout file, such as use the actual font itself (not artificial font) and provide the link to your document, is this because pica user are still stuck back in the traditional prepress environment.? "Sub" <submariner2@yahoo.com> wrote in message news:1142358808.884058.36760@u72g2000cwu.googlegroups.com... > Picas user vs. Inches user > > I al...

US-TX-Austin: Embedded SW Applications Eng., Design/Dev exp, user interface; Per (45347214410)
US-TX-Austin: Embedded SW Applications Eng., Design/Dev exp, user interface; Per (45347214410) ============================================================================================== Position: Embedded SW Applications Reference: SMC01981 Location: Austin TX Duration: Perm Skills: Exp working with customers directly on technical issues Exp issuing software releases to customers Exp helping to define and capture customer requirements along with past record of meeting development schedules Design and...

User defined type not defined
Hello all, I am trying to write a prog that autamatically sends emails ... Here is what I have done so far In the module section I have put this: Public Function SendEmail(email As String, subject As String, body As String, attach As String) Dim Mail As MailSender Set Mail = New MailSender Mail.Host = "relay.skynet.be" Mail.From = "my.emailaddress@myprovider.be" Mail.AddAddress (email) Mail.subject = subject Mail.body = body Mail.AddAttachment (attach) Mail.Send End Function Then in the program I assign values to the variables email,subject and body Next I call the fun...

Linux user's thoughts on OS X/iBook
Over the Christmas holidays, while visiting family, I recently helped my youngest brother choose to buy a new iBook G4 800MHz 12" as he is applying to law school. My background: 100% Linux user at home for eight years this month. Numerous years' experience with PCs of all types, plus lots of Mac experience back in the System 7.5.x days in a part-time job all through college. His background: Typical MS Word-and-Web-browser-on-PC stuff, though he did have an account on my Linux box for the usual word processor-and-browser uses during high school. I would never, ever have advised him to...

[ace-users] Re: _validate_connection Question
Hi Abdul, Thanks for using the PRF! >> ACE VERSION: 5.4.9 >> >> HOST MACHINE and OPERATING SYSTEM: >> Linux 2.6.9-22.0.1.ELsmp >> >> TARGET MACHINE and OPERATING SYSTEM, if different from HOST: >> COMPILER NAME AND VERSION (AND PATCHLEVEL): >> Lynxos 4.0.0 >> g++ 3.2.2 >> >> THE $ACE_ROOT/ace/config.h FILE [if you use a link to a platform- >> specific file, simply state which one]: >> #include "ace/config-lynxos.h" >> >> THE $ACE_RO...

User accounts being locked out
We have approximately 80 users on a windows 2000 network with an NT 4.0 PDC and another NT 4.0 BDC. I have users suddenly being locked out in the middle of the day. They will be working then suddenly their account is locked out. It also seems like this is happening to only a few users, not all (yet). What the heck is going on? Has anyone else seen this before? Thanks "Bob" <Spam@spamyoutohell.com> wrote in message news:11179eu9bqqa857@corp.supernews.com... > We have approximately 80 users on a windows 2000 network with an NT 4.0 PDC > and another NT...

Re: Linux Users Get Nasty With Blizzard.
"DFS" <nospam@dfs.com> wrote in message news:mek7b8$bka$2@dont-email.me... > On 3/20/2015 11:48 PM, Kelsey Bjarnason wrote: >> [snips] >> >> On Fri, 20 Mar 2015 11:25:07 -0700, Steve Carroll wrote: >> >>>> The point is, for all their screaming about the supposed numbers, when >>>> pressed, they can *never* actually back up their claims. >>> >>> Not if "back up" involves a percentage with certainty attached to it, >>> no. >> >> Certainty? No, but that's not the ...

London RISC OS user group Monday 17 July; USB developments
The next meeting of the RISC OS User Group of London (ROUGOL) will be: Dave Higton presenting new USB devices Monday 17th July 2006, 7:45pm (informal discussions from about 6pm onwards) The Blue-Eyed Maid (upstairs) 173 Borough High Street London SE1 1HR Dave Higton has been responsible for a wide variety of RISC OS software, including !AntiSpam (now developed by Frank de Bruijn). Most recently, he has been working on drivers and supporting software for a number of USB devices under RISC OS. These include USB FM radios from D-Link and AVerMedia, for which Dave provides...

Drigmorn1 User Manual
First cut of the user manual for Drigmorn1 is now available here http://www.enterpoint.co.uk/component_replacements/Drigmorn1_User_Manual_Issue_1_00.pdf. We are planning an update of this with some more photos and diagrams to clarify bits and pieces in a few weeks. As always anything you think we missed let us know and we will try and get that into the next update. The XC3S500E version has now passed all design tests. XC3S100E version isn't far behind and we are still on target to release to customers this week. John Adair Enterpoint Ltd. ...

label using a custom defined counter
Hello! In a book Im working on, I have defined a new counter for automatic numbering of examples. I have also defined a command, \eksempel, where this counter is automatically updated. In the book, I would like to be able to \label and \ref my examples. \eksempel{example title \label{thisexample}} .....in example \ref{thisexample}, we see .... Alas, the \label command only relates to standard pre-defined counters. Is there a way of fourcing \label to use my example auto-numbers when defined within a call to \eksempel ? Thanks Michael michaelp <michaelp@hio.no> schrieb: &...

2005 Worldwide User Group (SGIUG) conference: 13-16 June, Munich
Hello everybody, Similarly to last year, let me invite you all to the third SGI Worldwide User Group Conference, which will be held 13-16 June 2005 in Munich, Germany. For the detailed information please visit: http://www.sgiug.org/ Until May 14th you are still eligible for the early bird registration fee. This Year's Must Attend Conference promises to be an exciting event with users assembling from around the globe to present and discuss the very latest advancements in: Application Optimization and Tuning Building and Administering Large-node Clusters Data Access Over Long Distan...

Recovering "user settings"
I screwed up - While trying to replace the RAM battery for my 32K card, I lost everything. However in the variables menu there is a variable called D.01 that seems to have retained my alarms and user settings. I would like to recover the user settings. Can anybody help? One of the group readers, Fabian, suggested the following to recover my alarms and it worked: "> > Try this > > > > In your D.01 folder, you have a variable called 'Alarms'. Recall that onto > > the stack. Put #640BEh on the stack. Execute SYSEVAL. Store the list into =D8 > the var...

User or Iderntity?
In my office we're going to try Pegasus (whch I've been using at home). Which is better: to have users for each e-mail account or to have identities? I like it that users can have different random taglines, for example. But I find switching a pain. I like identities in that they're all there at once, no fuss no muss. We're using 4.12 and not using Mercury (because we have Linux server). TIA Zzzzzzzzz On Fri, 30 Apr 2004 22:01:11 GMT, Zorro wrote: > In my office we're going to try Pegasus (whch I've been using at > home). Which is better...

Edit user dictionary?
I can't count how many times I accidentally hit "add" or something for a word I really just wanted changed with the correction I typed in, and now it's screwed up in my dictionary. Is there any way to edit and remove it, and better yet add a bunch of words manually - PM's dictionary SUCKS. BTW, is there a better/updated one available anywhere, and can I merge it with mine? That'd be the kicker if there was :) [POSTED TO comp.mail.pegasus-mail.ms-windows - REPLY ON USENET PLEASE] In <qhphr0113bhmtifitsdrk6m9snhndo2usv@4ax.com> on Thu, 09 Dec 2004 16:0...

user to user authentication
hi, I have 2 questions concerning user-to-user authentication. First, I'm currently trying to develop a client/server application that uses kerberos 'user-to-user' authentication mechanism. Looking at kerberos source code examples, I've seen that u2u authentication is implemented in a reversed way, that is to say client issues a recvauth(..) and server issues a sendauth(..). With this schema, server credential cache grows up for each connection as it has to get a new u2u TGS for clients that don't send an previously sent TGT. I would like to implement u2u authenticati...

[tao-users] TAO & JacORB regarding CosTransactions/anonymous types
--_000_5F7142A76E33E84595B095DF911BFAFC2B56050DMSKEXCHswswsoft_ Content-Type: text/plain; charset="koi8-r" Content-Transfer-Encoding: quoted-printable Hello All, We are trying to interconnect C++ client on TAO 2.0.5 and JacORB-based Java= server. We use CosTransactions (OTS) for transaction management, and corre= sponding interceptors/codecs for service context passing. However, when call with OTS context reaches the server interceptor (JacOrb)= , it fails to decode it correctly. According to our investigation, the prob= lem happens when server interceptor reads '...

Securing Postgres Installation on multi-user server
Anybody knows, how to set-up secure postgres server in multi-user environment correctly? In my case postgres should start automatically, but any user has to supply password in order to connect. I've tried to use: local all pgsql ident sameuser local all all md5 as pg_hba.conf configuration - it worked in 7.3.x releases, but doesn't work now - server could not start - it writes a lot of: FATAL: 08P01: invalid frontend message type 0 LOCATION: SocketBackend, postgres.c:366 error messages. Does anybody know what kind of error it is? Thanks in advance, Alex. P.S.: I'm using F...

Applet user.region and user.timezone
Hi, I am trying to get a Java applet working on Windows and Linux. The problem is that it requires specific values to be set for user.reqion and user.timezone. On Windows I can set these in the Java Plug-In control panel. This is not ideal but at least it gets the job done (it means all applets will get the same fixed values for these properties), it would be nice to be able to set them on a per-applet basis. I've so far not found a way to do the same for Linux. Here, user.timezone is set, but it's a blank string. user.region is not even in the System properties. I thought that th...

Question for STOW users -- libtool conflict with make install prefix for stow directory
A question for all you users who like to use STOW <http://www.gnu.org/software/stow/> to keep your /usr/local software installations under administrative control. The usual course of action is to unpack a software archive, cd into the unpacked software directory, then ./configure --prefix=/usr/local{/X11R6 or whatever} make and then make prefix=/usr/local/stow/package_name/{/X11R6 or whatever} to install and then run stow on that package name. In an increasing number of software builds for libraries I am running into difficulties...

Question Regarding Using Constants to Define Boundries for Arrays
Hello, I wrote some software in Visual Basic 5 that I need to use arrays of varying sizes. The arrays need to have defined boundries, (i.e., Array(0 to UpperLimit)), because they will take input from an ActiveX controller which requires it. My question is... I need a Constant expression in order to define the UpperLimit value of the array, but I also need to manipulate it during runtime. Is there any way to do this? Thanks to whomever resoponds to this posting. Help would be greatly appreciated! Thanks, Dan Correction: I wrote it in Visual Basic 6 "UpliftMofo" <danie...

oracle forms user?
Hi I have an oracle form that needs to write a text file on another windows server. I can't get it to write because it seems there is a permission problem. If we grant full control on the folder to the Windows "Everyone" group, then it works fine. However, in practice, we don't want to grant full control to "Everyone". So my question is, what user or group should be given the proper privileges so that the form can write to the text file? Our form is a 10g form, which we run off Forms and Reports Services (10g). We use Windows 2003 servers. Forms and Reports runs o...

[ace-users] Re: Details
This is a multipart message in MIME format --_NextPart_000_000AE0F9 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Please see the attached file for details. --_NextPart_000_000AE0F9-- ...