Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9787 > unrolled thread

issues with const and iterator

Started by"hweekuan@yahoo.com" <hweekuan2011@gmail.com>
First post2011-09-06 02:46 -0700
Last post2011-09-06 16:11 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  issues with const and iterator "hweekuan@yahoo.com" <hweekuan2011@gmail.com> - 2011-09-06 02:46 -0700
    Re: issues with const and iterator Ian Collins <ian-news@hotmail.com> - 2011-09-06 21:56 +1200
      Re: issues with const and iterator James Kanze <james.kanze@gmail.com> - 2011-09-06 15:24 -0700
        Re: issues with const and iterator Ian Collins <ian-news@hotmail.com> - 2011-09-07 10:56 +1200
          Re: issues with const and iterator red floyd <no.spam.here@its.invalid> - 2011-09-06 16:11 -0700

#9787 — issues with const and iterator

From"hweekuan@yahoo.com" <hweekuan2011@gmail.com>
Date2011-09-06 02:46 -0700
Subjectissues with const and iterator
Message-ID<7ba85c39-9ae1-46d4-9a8f-7f0987a2c4a9@m3g2000pri.googlegroups.com>
i am using some code from a library in which i do not want to edit.
building on the library i have some compile time errors about
assigning iterators. below is the distilled code and error message.
appreciate your help:

  1 #include <vector>
  2 // --------------------------------------
  3 // struct B cannot be edited
  4 // --------------------------------------
  5 template<class D>
  6 struct B {
  7
  8   B(): data(5) { }
  9   const D& get() const { return data; }
 10   D data;
 11 };
 12 // --------------------------------------
 13 // edit struct C to make the code compile
 14 // --------------------------------------
 15 struct C {
 16
 17   typedef std::vector<int> D;
 18   typedef D::iterator      Ditr;
 19
 20   C () {
 21     bd = new B<D>();
 22     begin_itr = (bd->get()).begin(); // error happens here
 23   }
 24   Ditr   begin_itr;
 25   B<D> * bd;
 26 };
 27 // --------------------------------------
 28 int main ( ) { }


g++ -Wall vector_t.C
vector_t.C: In constructor ‘C::C()’:
vector_t.C:22: error: no match for ‘operator=’ in ‘((C*)this)-
>C::begin_itr = ((const std::vector<int, std::allocator<int> >*)
((C*)this)->C::bd-> B<D>::get [with D = std::vector<int,
std::allocator<int> >]())->std::vector<_Tp, _Alloc>::begin [with _Tp =
int, _Alloc = std::allocator<int>]()’
/usr/include/c++/4.2.1/bits/stl_iterator.h:637: note: candidates are:
__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >& __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >::operator=(const
__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >&)

[toc] | [next] | [standalone]


#9788

FromIan Collins <ian-news@hotmail.com>
Date2011-09-06 21:56 +1200
Message-ID<9cm927F72aU3@mid.individual.net>
In reply to#9787
On 09/ 6/11 09:46 PM, hweekuan@yahoo.com wrote:
> i am using some code from a library in which i do not want to edit.
> building on the library i have some compile time errors about
> assigning iterators. below is the distilled code and error message.
> appreciate your help:

Answer at the end!

>    1 #include<vector>
>    2 // --------------------------------------
>    3 // struct B cannot be edited
>    4 // --------------------------------------
>    5 template<class D>
>    6 struct B {
>    7
>    8   B(): data(5) { }
>    9   const D&  get() const { return data; }

Returns a const object.

>   10   D data;
>   11 };
>   12 // --------------------------------------
>   13 // edit struct C to make the code compile
>   14 // --------------------------------------
>   15 struct C {
>   16
>   17   typedef std::vector<int>  D;
>   18   typedef D::iterator      Ditr;
>   19
>   20   C () {
>   21     bd = new B<D>();
>   22     begin_itr = (bd->get()).begin(); // error happens here

(bd->get()).begin() returns a const_iterator.

>   23   }
>   24   Ditr   begin_itr;
>   25   B<D>  * bd;
>   26 };
>   27 // --------------------------------------
>   28 int main ( ) { }

Line numbers in Usenet posts are a bad idea if you want people to try 
your code!

> g++ -Wall vector_t.C
> vector_t.C: In constructor ‘C::C()’:
> vector_t.C:22: error: no match for ‘operator=’ in ‘((C*)this)-
>> C::begin_itr = ((const std::vector<int, std::allocator<int>  >*)
> ((C*)this)->C::bd->  B<D>::get [with D = std::vector<int,
> std::allocator<int>  >]())->std::vector<_Tp, _Alloc>::begin [with _Tp =
> int, _Alloc = std::allocator<int>]()’
> /usr/include/c++/4.2.1/bits/stl_iterator.h:637: note: candidates are:
> __gnu_cxx::__normal_iterator<int*, std::vector<int,
> std::allocator<int>  >  >&  __gnu_cxx::__normal_iterator<int*,
> std::vector<int, std::allocator<int>  >  >::operator=(const
> __gnu_cxx::__normal_iterator<int*, std::vector<int,
> std::allocator<int>  >  >&)

In it's own ridiculously long winded way, g++ is telling you you can't 
assign a const iterator to an iterator!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#9838

FromJames Kanze <james.kanze@gmail.com>
Date2011-09-06 15:24 -0700
Message-ID<03a5f937-bae3-4ddf-ba0d-ea32aaa0e36c@1g2000vbu.googlegroups.com>
In reply to#9788
On Sep 6, 10:56 am, Ian Collins <ian-n...@hotmail.com> wrote:
> On 09/ 6/11 09:46 PM, hweek...@yahoo.com wrote:

    [...]
> Line numbers in Usenet posts are a bad idea if you want people to try
> your code!

But they're a good idea if you're also posting error messages,
so that people can find the line the message is referring to.
(And it doesn't require any real effort to remove them if you
want to try the code.)

--
James Kanze

[toc] | [prev] | [next] | [standalone]


#9846

FromIan Collins <ian-news@hotmail.com>
Date2011-09-07 10:56 +1200
Message-ID<9cnmotF72aU10@mid.individual.net>
In reply to#9838
On 09/ 7/11 10:24 AM, James Kanze wrote:
> On Sep 6, 10:56 am, Ian Collins<ian-n...@hotmail.com>  wrote:
>> On 09/ 6/11 09:46 PM, hweek...@yahoo.com wrote:
>
>      [...]
>> Line numbers in Usenet posts are a bad idea if you want people to try
>> your code!
>
> But they're a good idea if you're also posting error messages,
> so that people can find the line the message is referring to.

He added a comment:

  22     begin_itr = (bd->get()).begin(); // error happens here

> (And it doesn't require any real effort to remove them if you
> want to try the code.)

If you want help, make it easy.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#9851

Fromred floyd <no.spam.here@its.invalid>
Date2011-09-06 16:11 -0700
Message-ID<j469b2$c4i$1@dont-email.me>
In reply to#9846
On 9/6/2011 3:56 PM, Ian Collins wrote:
> On 09/ 7/11 10:24 AM, James Kanze wrote:
>> On Sep 6, 10:56 am, Ian Collins<ian-n...@hotmail.com> wrote:
>>> On 09/ 6/11 09:46 PM, hweek...@yahoo.com wrote:
>>
>> [...]
>>> Line numbers in Usenet posts are a bad idea if you want people to try
>>> your code!
>>
>> But they're a good idea if you're also posting error messages,
>> so that people can find the line the message is referring to.
>
> He added a comment:
>
> 22 begin_itr = (bd->get()).begin(); // error happens here
>
>> (And it doesn't require any real effort to remove them if you
>> want to try the code.)
>
> If you want help, make it easy.
>

And even better is:

begin_itr = (bd->get()).begin(); // LINE 22 - error happens here

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web