Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > weird error

Reply
Thread Tools

weird error

 
 
Joe Laughlin
Guest
Posts: n/a
 
      10-22-2004
(file and class names changed to protect the innocent)

g++ -Wall -c file.cpp: In member function `std::string
Some_Class::get_member(const std::string&) const':
file.cpp:46: passing `const std::map<std::string,
std::string, std::less<std::string>, std::allocator<std:air<const
std::string, std::string> > >' as `this' argument of `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>:perator[](const _Key&) [with _Key = std::string,
_Tp = std::string, _Compare = std::less<std::string>, _Alloc =
std::allocator<std:air<const std::string, std::string> >]' discards
qualifiers

I can't make heads or tails of this error. I'm guessing it has something to
do with calling a non-const function on some const object. Any ideas?

The function SomeClass::get_member is:

std::string SomeClass::get_member(const std::string &name) const
{
return _member[name];
}

where _member is a std::map<std::string, std::string>.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-22-2004
"Joe Laughlin" <(E-Mail Removed)> wrote...
> (file and class names changed to protect the innocent)
>
> g++ -Wall -c file.cpp: In member function `std::string
> Some_Class::get_member(const std::string&) const':
> file.cpp:46: passing `const std::map<std::string,
> std::string, std::less<std::string>, std::allocator<std:air<const
> std::string, std::string> > >' as `this' argument of `_Tp&
> std::map<_Key,
> _Tp, _Compare, _Alloc>:perator[](const _Key&) [with _Key =
> std::string,
> _Tp = std::string, _Compare = std::less<std::string>, _Alloc =
> std::allocator<std:air<const std::string, std::string> >]' discards
> qualifiers
>
> I can't make heads or tails of this error. I'm guessing it has something
> to
> do with calling a non-const function on some const object. Any ideas?
>
> The function SomeClass::get_member is:
>
> std::string SomeClass::get_member(const std::string &name) const
> {
> return _member[name];
> }
>
> where _member is a std::map<std::string, std::string>.


You declared this member function ('get_member') "const". That means that
the object (and all its data member except 'mutable' ones) are not going to
change. Indexing operator for 'map' template is a _non-const_ function.
I.e. it cannot be called for a const map. It has to be non-const because
it may insert a new object if it's not there yet.

You need to either make peace with the fact that it may insert another
object into your _member (and declare '_member' "mutable") or use some other
way. It seems that your function _assumes_ that there is an element with
the
key 'name' in the map. What if there isn't?

Victor


 
Reply With Quote
 
 
 
 
Brian Riis
Guest
Posts: n/a
 
      10-22-2004
Joe Laughlin wrote:
> (file and class names changed to protect the innocent)
>
> g++ -Wall -c file.cpp: In member function `std::string
> Some_Class::get_member(const std::string&) const':
> file.cpp:46: passing `const std::map<std::string,
> std::string, std::less<std::string>, std::allocator<std:air<const
> std::string, std::string> > >' as `this' argument of `_Tp& std::map<_Key,
> _Tp, _Compare, _Alloc>:perator[](const _Key&) [with _Key = std::string,
> _Tp = std::string, _Compare = std::less<std::string>, _Alloc =
> std::allocator<std:air<const std::string, std::string> >]' discards
> qualifiers
>
> I can't make heads or tails of this error. I'm guessing it has something to
> do with calling a non-const function on some const object. Any ideas?
>
> The function SomeClass::get_member is:
>
> std::string SomeClass::get_member(const std::string &name) const
> {
> return _member[name];


Your function is declared const, but you cannot guarantee that the
subscript operator of the map doesn't change the value at 'name'. In
other words: In a const member function you can only use other const
functions (in regard to your own members). In the above, if 'name' does
not exist in _member, *it will be created*. You are not allowed to do
that in a const function.

> }


try this:

std::string SomeClass::get_member(std::string const & name) const
{
_member.const_iterator it;
it = _member.find(name);
if(it != _member.end())
_return it->second;
else
<...> // Whatever you want to do, if 'name' isn't there...
}

Takes up a bit more space, but it works. On most compilers I should
think it is as efficient too.

--
/Brian Riis
 
Reply With Quote
 
Joe Laughlin
Guest
Posts: n/a
 
      10-22-2004
Brian Riis wrote:
> Joe Laughlin wrote:
>> (file and class names changed to protect the innocent)
>>
>> g++ -Wall -c file.cpp: In member function `std::string
>> Some_Class::get_member(const std::string&) const':
>> file.cpp:46: passing `const std::map<std::string,
>> std::string, std::less<std::string>,
>> std::allocator<std:air<const std::string,
>> std::string> > >' as `this' argument of `_Tp&
>> std::map<_Key, _Tp, _Compare,
>> _Alloc>:perator[](const _Key&) [with _Key =
>> std::string, _Tp = std::string, _Compare =
>> std::less<std::string>, _Alloc =
>> std::allocator<std:air<const std::string, std::string>
>> >]' discards qualifiers

>>
>> I can't make heads or tails of this error. I'm guessing
>> it has something to do with calling a non-const function
>> on some const object. Any ideas?
>>
>> The function SomeClass::get_member is:
>>
>> std::string SomeClass::get_member(const std::string
>> &name) const {
>> return _member[name];

>
> Your function is declared const, but you cannot guarantee
> that the subscript operator of the map doesn't change the
> value at 'name'. In other words: In a const member
> function you can only use other const functions (in
> regard to your own members). In the above, if 'name' does
> not exist in _member, *it will be created*. You are not
> allowed to do that in a const function.
>
>> }

>
> try this:
>
> std::string SomeClass::get_member(std::string const &
> name) const {
> _member.const_iterator it;
> it = _member.find(name);
> if(it != _member.end())
> _return it->second;
> else
> <...> // Whatever you want to do, if 'name' isn't
> there... }
>
> Takes up a bit more space, but it works. On most
> compilers I should think it is as efficient too.


Thanks you guys.

Is:
_member.const_iterator it;
legal? The compiler didn't like that line, but it did like:
std::map<std::string, std::string>::const_iterator it;

Joe



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-22-2004
>>
>> try this:
>>
>> std::string SomeClass::get_member(std::string const &
>> name) const {
>> _member.const_iterator it;
>> it = _member.find(name);
>> if(it != _member.end())
>> _return it->second;
>> else
>> <...> // Whatever you want to do, if 'name' isn't
>> there... }
>>
>> Takes up a bit more space, but it works. On most
>> compilers I should think it is as efficient too.

>
> Thanks you guys.
>
> Is:
> _member.const_iterator it;
> legal?


No

> The compiler didn't like that line, but it did like:
> std::map<std::string, std::string>::const_iterator it;
>


I'm sure that's what Brian meant to say.

john


 
Reply With Quote
 
Joe Laughlin
Guest
Posts: n/a
 
      10-22-2004
Joe Laughlin wrote:
> (file and class names changed to protect the innocent)
>
> g++ -Wall -c file.cpp: In member function `std::string
> Some_Class::get_member(const std::string&) const':
> file.cpp:46: passing `const std::map<std::string,
> std::string, std::less<std::string>,
> std::allocator<std:air<const std::string,
> std::string> > >' as `this' argument of `_Tp&
> std::map<_Key, _Tp, _Compare,
> _Alloc>:perator[](const _Key&) [with _Key =
> std::string, _Tp = std::string, _Compare =
> std::less<std::string>, _Alloc =
> std::allocator<std:air<const std::string, std::string>
> >]' discards qualifiers

>
> I can't make heads or tails of this error. I'm guessing
> it has something to do with calling a non-const function
> on some const object. Any ideas?
>
> The function SomeClass::get_member is:
>
> std::string SomeClass::get_member(const std::string
> &name) const {
> return _member[name];
> }
>
> where _member is a std::map<std::string, std::string>.


Here's another one, probably related to my lack of understanding about the
STL. (These error messages sure aren't very clear!)


Error is:
could not convert `
(&iter)->std::_List_iterator<_Tp, _Ref, _Ptr>:perator*() const [with
_Tp =
SomeClass, _Ref = const SomeClass&, _Ptr = const
SomeClass*]()' to `SomeClass&'

SomeClass is a std::list of a user-defined class.

The error gets raised on the return statement of this function:
SomeClass& AnotherClass::get_element(const string& tag) const
{
std::list<SomeClass>::const_iterator iter = _elements.begin();

// This isn't very well written, and I'm sure there's better
// ways of doing it. Feel free to give me ideas on how to improve it.
while (iter != _elements.end())
{
if (iter->get_member("tag") == tag)
return *iter; // error rasied here
++iter;
}
// throw exception cuz not found
}


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-22-2004
Joe Laughlin wrote:
> Joe Laughlin wrote:
>
>>(file and class names changed to protect the innocent)
>>
>>g++ -Wall -c file.cpp: In member function `std::string
>> Some_Class::get_member(const std::string&) const':
>>file.cpp:46: passing `const std::map<std::string,
>> std::string, std::less<std::string>,
>> std::allocator<std:air<const std::string,
>> std::string> > >' as `this' argument of `_Tp&
>> std::map<_Key, _Tp, _Compare,
>> _Alloc>:perator[](const _Key&) [with _Key =
>> std::string, _Tp = std::string, _Compare =
>>std::less<std::string>, _Alloc =
>>std::allocator<std:air<const std::string, std::string>
>>
>>>]' discards qualifiers

>>
>>I can't make heads or tails of this error. I'm guessing
>>it has something to do with calling a non-const function
>>on some const object. Any ideas?
>>
>>The function SomeClass::get_member is:
>>
>>std::string SomeClass::get_member(const std::string
>>&name) const {
>> return _member[name];
>>}
>>
>>where _member is a std::map<std::string, std::string>.

>
>
> Here's another one, probably related to my lack of understanding about the
> STL. (These error messages sure aren't very clear!)
>
>
> Error is:
> could not convert `
> (&iter)->std::_List_iterator<_Tp, _Ref, _Ptr>:perator*() const [with
> _Tp =
> SomeClass, _Ref = const SomeClass&, _Ptr = const
> SomeClass*]()' to `SomeClass&'
>
> SomeClass is a std::list of a user-defined class.
>
> The error gets raised on the return statement of this function:
> SomeClass& AnotherClass::get_element(const string& tag) const
> {
> std::list<SomeClass>::const_iterator iter = _elements.begin();
>
> // This isn't very well written, and I'm sure there's better
> // ways of doing it. Feel free to give me ideas on how to improve it.
> while (iter != _elements.end())
> {
> if (iter->get_member("tag") == tag)
> return *iter; // error rasied here
> ++iter;
> }
> // throw exception cuz not found
> }


Your 'const' is all screwed up again. The return value of this function
is 'SomeClass&', a reference to non-const SomeClass object. The 'iter'
is declared as 'const_iterator', whose operator* (dereference operator)
yields a 'SomeClass const&', a reference to a const SomeClass object.
Of course it cannot convert a const ref to a non-const ref.

Victor
 
Reply With Quote
 
Joe Laughlin
Guest
Posts: n/a
 
      10-22-2004
Victor Bazarov wrote:
> Joe Laughlin wrote:
>> Joe Laughlin wrote:
>>
>>> (file and class names changed to protect the innocent)
>>>
>>> g++ -Wall -c file.cpp: In member function `std::string
>>> Some_Class::get_member(const std::string&) const':
>>> file.cpp:46: passing `const std::map<std::string,
>>> std::string, std::less<std::string>,
>>> std::allocator<std:air<const std::string,
>>> std::string> > >' as `this' argument of `_Tp&
>>> std::map<_Key, _Tp, _Compare,
>>> _Alloc>:perator[](const _Key&) [with _Key =
>>> std::string, _Tp = std::string, _Compare =
>>> std::less<std::string>, _Alloc =
>>> std::allocator<std:air<const std::string, std::string>
>>>
>>>> ]' discards qualifiers
>>>
>>> I can't make heads or tails of this error. I'm guessing
>>> it has something to do with calling a non-const function
>>> on some const object. Any ideas?
>>>
>>> The function SomeClass::get_member is:
>>>
>>> std::string SomeClass::get_member(const std::string
>>> &name) const {
>>> return _member[name];
>>> }
>>>
>>> where _member is a std::map<std::string, std::string>.

>>
>>
>> Here's another one, probably related to my lack of
>> understanding about the STL. (These error messages sure
>> aren't very clear!)
>>
>>
>> Error is:
>> could not convert `
>> (&iter)->std::_List_iterator<_Tp, _Ref,
>> _Ptr>:perator*() const [with _Tp =
>> SomeClass, _Ref = const SomeClass&, _Ptr = const
>> SomeClass*]()' to `SomeClass&'
>>
>> SomeClass is a std::list of a user-defined class.
>>
>> The error gets raised on the return statement of this
>> function: SomeClass& AnotherClass::get_element(const
>> string& tag) const {
>> std::list<SomeClass>::const_iterator iter =
>> _elements.begin();
>>
>> // This isn't very well written, and I'm sure
>> there's better // ways of doing it. Feel free to
>> give me ideas on how to improve it. while (iter !=
>> _elements.end()) {
>> if (iter->get_member("tag") == tag)
>> return *iter; // error rasied here
>> ++iter;
>> }
>> // throw exception cuz not found
>> }

>
> Your 'const' is all screwed up again. The return value
> of this function is 'SomeClass&', a reference to
> non-const SomeClass object. The 'iter' is declared as
> 'const_iterator', whose operator* (dereference operator)
> yields a 'SomeClass const&', a reference to a const
> SomeClass object. Of course it cannot convert a const ref
> to a non-const ref.
>
> Victor


Ah, I understand. So what's the correct thing to do here? (Can I return a
const reference?)


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-22-2004
Joe Laughlin wrote:
> Victor Bazarov wrote:
>
>>Joe Laughlin wrote:
>>
>>>Joe Laughlin wrote:
>>>
>>>
>>>>(file and class names changed to protect the innocent)
>>>>
>>>>g++ -Wall -c file.cpp: In member function `std::string
>>>> Some_Class::get_member(const std::string&) const':
>>>>file.cpp:46: passing `const std::map<std::string,
>>>> std::string, std::less<std::string>,
>>>> std::allocator<std:air<const std::string,
>>>> std::string> > >' as `this' argument of `_Tp&
>>>> std::map<_Key, _Tp, _Compare,
>>>> _Alloc>:perator[](const _Key&) [with _Key =
>>>> std::string, _Tp = std::string, _Compare =
>>>>std::less<std::string>, _Alloc =
>>>>std::allocator<std:air<const std::string, std::string>
>>>>
>>>>>]' discards qualifiers
>>>>
>>>>I can't make heads or tails of this error. I'm guessing
>>>>it has something to do with calling a non-const function
>>>>on some const object. Any ideas?
>>>>
>>>>The function SomeClass::get_member is:
>>>>
>>>>std::string SomeClass::get_member(const std::string
>>>>&name) const {
>>>> return _member[name];
>>>>}
>>>>
>>>>where _member is a std::map<std::string, std::string>.
>>>
>>>
>>>Here's another one, probably related to my lack of
>>>understanding about the STL. (These error messages sure
>>>aren't very clear!)
>>>
>>>
>>>Error is:
>>>could not convert `
>>> (&iter)->std::_List_iterator<_Tp, _Ref,
>>>_Ptr>:perator*() const [with _Tp =
>>> SomeClass, _Ref = const SomeClass&, _Ptr = const
>>> SomeClass*]()' to `SomeClass&'
>>>
>>>SomeClass is a std::list of a user-defined class.
>>>
>>>The error gets raised on the return statement of this
>>>function: SomeClass& AnotherClass::get_element(const
>>>string& tag) const {
>>> std::list<SomeClass>::const_iterator iter =
>>>_elements.begin();
>>>
>>> // This isn't very well written, and I'm sure
>>> there's better // ways of doing it. Feel free to
>>> give me ideas on how to improve it. while (iter !=
>>> _elements.end()) {
>>> if (iter->get_member("tag") == tag)
>>> return *iter; // error rasied here
>>> ++iter;
>>> }
>>> // throw exception cuz not found
>>>}

>>
>>Your 'const' is all screwed up again. The return value
>>of this function is 'SomeClass&', a reference to
>>non-const SomeClass object. The 'iter' is declared as
>>'const_iterator', whose operator* (dereference operator)
>>yields a 'SomeClass const&', a reference to a const
>>SomeClass object. Of course it cannot convert a const ref
>>to a non-const ref.
>>
>>Victor

>
>
> Ah, I understand. So what's the correct thing to do here? (Can I return a
> const reference?)


You can of course. I don't know if your algorithm allows you to, though.
Why did you need a non-const ref before?

V
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-22-2004
"Joe Laughlin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Victor Bazarov wrote:
> > Your 'const' is all screwed up again. The return value
> > of this function is 'SomeClass&', a reference to
> > non-const SomeClass object. The 'iter' is declared as
> > 'const_iterator', whose operator* (dereference operator)
> > yields a 'SomeClass const&', a reference to a const
> > SomeClass object. Of course it cannot convert a const ref
> > to a non-const ref.
> >
> > Victor

>
> Ah, I understand. So what's the correct thing to do here?


return a const reference.

> (Can I return a
> const reference?)




-Mike


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A Weird Appearance for a Weird Site David Segall HTML 0 01-22-2011 04:50 AM
Re: A Weird Appearance for a Weird Site Beauregard T. Shagnasty HTML 1 01-21-2011 04:17 PM
Re: A Weird Appearance for a Weird Site richard HTML 0 01-21-2011 07:10 AM
Re: A Weird Appearance for a Weird Site dorayme HTML 1 01-21-2011 06:51 AM
Re: A Weird Appearance for a Weird Site richard HTML 0 01-21-2011 06:46 AM



Advertisments