| View previous topic :: View next topic |
| Author |
Message |
Kevin McDermott Guest
|
Posted: Sun Dec 14, 2003 2:01 am Post subject: Question about Inheritance and stl containers |
|
|
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
|
Posted: Sun Dec 14, 2003 11:08 am Post subject: Re: Question about Inheritance and stl containers |
|
|
"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
|
Posted: Sun Dec 14, 2003 11:08 am Post subject: Re: Question about Inheritance and stl containers |
|
|
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
|
Posted: Sun Dec 14, 2003 11:14 am Post subject: Re: Question about Inheritance and stl containers |
|
|
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
|
Posted: Sun Dec 14, 2003 11:15 am Post subject: Re: Question about Inheritance and stl containers |
|
|
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 |
|
 |
|