C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about Inheritance and stl containers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Kevin McDermott
Guest





PostPosted: Sun Dec 14, 2003 2:01 am    Post subject: Question about Inheritance and stl containers Reply with quote



I get this error message when I compile my program on Visual Studio.

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'std::list<_Ty>::const_iterator' (or there is no
acceptable conversion)
with
[
_Ty=Element *
]

I have a base class called Element, and derived classes ComplexElement
and SimpleElement. ComplexElement has a member variable
list<Element*> subElements. I get the above error when I try to do
this

list<Element*>::iterator pos;
pos = subElements.begin();

I have copy constructors and assignment operators defined for each
class.

Am I missing something??

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jonathan Turkanis
Guest





PostPosted: Sun Dec 14, 2003 11:08 am    Post subject: Re: Question about Inheritance and stl containers Reply with quote



"Kevin McDermott" <kmcdermott63 (AT) comcast (DOT) net> wrote

Quote:
I get this error message when I compile my program on Visual Studio.

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'std::list<_Ty>::const_iterator' (or there is no
acceptable conversion)
with
[
_Ty=Element *
]

I have a base class called Element, and derived classes ComplexElement
and SimpleElement. ComplexElement has a member variable
list<Element*> subElements. I get the above error when I try to do
this

list<Element*>::iterator pos;
pos = subElements.begin();


std::list -- like all the other STL containers -- has two member functions
'begin': one is non-const and returns a std::list::<...>::iterator, the
other is const and returns a std::list::<...>::const_iterator:

iterator begin();
const_iterator begin() const;

If you write

subElements.begin();

from within the body of a const member function, subElements has type const
std::list<Element*>, so the const version of begin is called. Try:

list<Element*>::const_iterator pos = subElements.begin();



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Dennett
Guest





PostPosted: Sun Dec 14, 2003 11:08 am    Post subject: Re: Question about Inheritance and stl containers Reply with quote



Kevin McDermott wrote:

Quote:
I get this error message when I compile my program on Visual Studio.

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'std::list<_Ty>::const_iterator' (or there is no
acceptable conversion)
with
[
_Ty=Element *
]

I have a base class called Element, and derived classes ComplexElement
and SimpleElement. ComplexElement has a member variable
list<Element*> subElements. I get the above error when I try to do
this

list<Element*>::iterator pos;
pos = subElements.begin();

I have copy constructors and assignment operators defined for each
class.

Am I missing something??

The compiler error message is saying that the right hand
operand (subElements.begin()) is a const_iterator, not a
list<Element*>::iterator. You can convert from an iterator
to a const_iterator, but the other way around would violate
const-correctness.

If you declared instead
std::list<Element*>::const_iterator pos(subElements.begin());
things might work out better. If you really need a non-const
iterator into subElements, there's a broader problem to
address.

-- James.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Sun Dec 14, 2003 11:14 am    Post subject: Re: Question about Inheritance and stl containers Reply with quote

On 13 Dec 2003 21:01:40 -0500, [email]kmcdermott63 (AT) comcast (DOT) net[/email] (Kevin
McDermott) wrote:

Quote:
error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'std::list<_Ty>::const_iterator'

Note the const_iterator.

Quote:
I have a base class called Element, and derived classes ComplexElement
and SimpleElement. ComplexElement has a member variable
list<Element*> subElements. I get the above error when I try to do
this

list<Element*>::iterator pos;

Note the plain iterator.

Quote:
pos = subElements.begin();

This statement is in code where subElements is const. A likely place
is in a const member function. There is no conversion from a
const_iterator to iterator because it discards constness. You need to
either remove the const from the member function or make pos a
const_iterator.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Sun Dec 14, 2003 11:15 am    Post subject: Re: Question about Inheritance and stl containers Reply with quote

Kevin McDermott wrote:
Quote:
I get this error message when I compile my program on Visual Studio.

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'std::list<_Ty>::const_iterator' (or there is no
acceptable conversion)
with
[
_Ty=Element *
]
... [snipped] ...
list<Element*>::iterator pos;
pos = subElements.begin();
...

The error message says that you can't assign a const_iterator to
a plain iterator. Nothing to do with inheritance.

This suggests that subElements is const in the context where the
statement is refusing to compile.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.