473,881 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weird error

(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::s tring,
std::string, std::less<std:: string>, std::allocator< std::pair<const
std::string, std::string> > >' as `this' argument of `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>::operat or[](const _Key&) [with _Key = std::string,
_Tp = std::string, _Compare = std::less<std:: string>, _Alloc =
std::allocator< std::pair<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::s tring, std::string>.
Jul 22 '05 #1
18 2384
"Joe Laughlin" <Jo************ ***@boeing.com> 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::s tring,
std::string, std::less<std:: string>, std::allocator< std::pair<const
std::string, std::string> > >' as `this' argument of `_Tp&
std::map<_Key,
_Tp, _Compare, _Alloc>::operat or[](const _Key&) [with _Key =
std::string,
_Tp = std::string, _Compare = std::less<std:: string>, _Alloc =
std::allocator< std::pair<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::s tring, 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
Jul 22 '05 #2
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::s tring,
std::string, std::less<std:: string>, std::allocator< std::pair<const
std::string, std::string> > >' as `this' argument of `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>::operat or[](const _Key&) [with _Key = std::string,
_Tp = std::string, _Compare = std::less<std:: string>, _Alloc =
std::allocator< std::pair<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::str ing const & name) const
{
_member.const_i terator it;
it = _member.find(na me);
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
Jul 22 '05 #3
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::s tring,
std::string, std::less<std:: string>,
std::allocator< std::pair<const std::string,
std::string> > >' as `this' argument of `_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operat or[](const _Key&) [with _Key =
std::string, _Tp = std::string, _Compare =
std::less<std:: string>, _Alloc =
std::allocator< std::pair<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::str ing const &
name) const {
_member.const_i terator it;
it = _member.find(na me);
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_i terator it;
legal? The compiler didn't like that line, but it did like:
std::map<std::s tring, std::string>::c onst_iterator it;

Joe

Jul 22 '05 #4
>>
try this:

std::string SomeClass::get_ member(std::str ing const &
name) const {
_member.const_i terator it;
it = _member.find(na me);
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_i terator it;
legal?


No
The compiler didn't like that line, but it did like:
std::map<std::s tring, std::string>::c onst_iterator it;


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

john
Jul 22 '05 #5
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::s tring,
std::string, std::less<std:: string>,
std::allocator< std::pair<const std::string,
std::string> > >' as `this' argument of `_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operat or[](const _Key&) [with _Key =
std::string, _Tp = std::string, _Compare =
std::less<std:: string>, _Alloc =
std::allocator< std::pair<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::s tring, 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_ite rator<_Tp, _Ref, _Ptr>::operator *() 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::g et_element(cons t string& tag) const
{
std::list<SomeC lass>::const_it erator 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("ta g") == tag)
return *iter; // error rasied here
++iter;
}
// throw exception cuz not found
}
Jul 22 '05 #6
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::s tring,
std::string, std::less<std:: string>,
std::allocator< std::pair<const std::string,
std::string> > >' as `this' argument of `_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operat or[](const _Key&) [with _Key =
std::string, _Tp = std::string, _Compare =
std::less<std ::string>, _Alloc =
std::allocato r<std::pair<con st 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::s tring, 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_ite rator<_Tp, _Ref, _Ptr>::operator *() 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::g et_element(cons t string& tag) const
{
std::list<SomeC lass>::const_it erator 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("ta g") == 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
Jul 22 '05 #7
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::s tring,
std::string, std::less<std:: string>,
std::allocator< std::pair<const std::string,
std::string> > >' as `this' argument of `_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operat or[](const _Key&) [with _Key =
std::string, _Tp = std::string, _Compare =
std::less<std:: string>, _Alloc =
std::allocator< std::pair<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::s tring, 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_ite rator<_Tp, _Ref,
_Ptr>::operator *() 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::g et_element(cons t
string& tag) const {
std::list<SomeC lass>::const_it erator 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("ta g") == 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?)
Jul 22 '05 #8
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::s tring,
std::string, std::less<std:: string>,
std::allocator< std::pair<const std::string,
std::string> > >' as `this' argument of `_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operat or[](const _Key&) [with _Key =
std::string, _Tp = std::string, _Compare =
std::less<s td::string>, _Alloc =
std::alloca tor<std::pair<c onst 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::stri ng SomeClass::get_ member(const std::string
&name) const {
return _member[name];
}

where _member is a std::map<std::s tring, std::string>.
Here's another one, probably related to my lack of
understandin g about the STL. (These error messages sure
aren't very clear!)
Error is:
could not convert `
(&iter)->std::_List_ite rator<_Tp, _Ref,
_Ptr>::opera tor*() 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::g et_element(cons t
string& tag) const {
std::list<SomeC lass>::const_it erator iter =
_elements.be gin();

// 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("ta g") == 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_iterat or', 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
Jul 22 '05 #9
"Joe Laughlin" <Jo************ ***@boeing.com> wrote in message
news:I6******** @news.boeing.co m...
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
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2083
by: redneck_kiwi | last post by:
Hi all: I have a really weird problem. I am developing a customer catalog system for my company and as such have delved into sessions for authentication and access levels. So far, I have managed to get a working system just about finished. I am building an interface for our customer service folks to use to manage registered customers and am seeing some weird behavior.
5
14637
by: NanQuan | last post by:
I'm hoping someone can help me solve this error since I am at a total loss here. Usually I don't bother posting on any forums or groups on the internet and prefer to solve stuff myself but this is a total mistery. I have a function inside an ASP page as a result of which I get the following error message: Microsoft VBScript compilation error '800a03ea'
1
2790
by: amit | last post by:
I am trying to compile the sample program genwin.sqc, using nsqlprep which is used to precompile embedded sql in C. I am getting weird errors and that is because windows.h is included in the genwin.sqc file. I am using Setting the lib and include path: set INCLUDE=C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\include;C:\Program Files\Microsoft SQL Server\80\Tools\DevTools\Samples\esqlc;%include%
2
2541
by: Kathy Houtami | last post by:
Hi there I've been encountered with weird compile error using Access 97. The error message is "Member or data member is not found" and it highlighted a line of code that has not be changed and has been compiled with no error for days. The code was Me.CircuitQualifier.Requery
6
1553
by: gh0st54 | last post by:
Hi I have a weird javascript error that only happens on my live server when i open the page http://www.imrated.co.uk/reg.aspx i get the error 'Problems with this page ... blablabla Line : 3 Char : 1 Error:Syntax error
0
2070
by: Alan Silver | last post by:
Hello, I have two weird problems here. I have a master page file that works absolutely fine. When I load it up in VWD, I get a couple of weird (to me) errors. First, I get the error "Unrecognized tag prefix or device filter 'asp'" on the third line shown below... <head runat="server">
0
1838
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this code SHOULD do: - read new (=updated) licensetext from file $license_path then - read and modify recursively all files from $current_dir, replacing old
7
2020
by: dtschoepe | last post by:
Hi, I am working on a project for school and I am trying to get my head around something weird. I have a function setup which is used to get an input from stdin and a piece of memory is created on the heap using malloc. This input is then run through a regex and another test. The result is passed back as a char pointer to the program for further processing The first time I call this function, it performs normally. However,
6
1881
by: =?Utf-8?B?amVmZmVyeQ==?= | last post by:
i need help with a combo box and this same code works on my first tab with a combo box. The error or problem i have is this code causes an index out of range error when i run it on my second combo box: (errored code) Select Case ComboBox2.text Case Is ="Execute Program" 'code to execute program here Case Is ="Other Command that executes code at a certain time" ' code below the same as above except that it works But not my first(this...
4
2629
by: d3vkit | last post by:
I recently had a friend pass along some web work for me; a simple update. I for some reason decided I would do some overhauling of the page (he had been doing straight html and I figured some php would make things MUCH more manageable, especially with the pictures). So, I set to work doing what I normally do for all my site, create the index page with a switch to stick different content in the main part of the page depending on variables passed...
0
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11095
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10812
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9552
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7952
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7108
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5780
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.