473,382 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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::string,
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>::operator[](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::string, std::string>.
Jul 22 '05 #1
18 2336
"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::string,
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>::operator[](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::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
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::string,
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>::operator[](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::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
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::string,
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>::operator[](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::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

Jul 22 '05 #4
>>
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
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::string,
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>::operator[](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::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>::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::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
}
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::string,
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>::operator[](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::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>::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::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
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::string,
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>::operator[](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::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>::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::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?)
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::string,
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>::operator[](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::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>::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::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
Jul 22 '05 #9
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:I6********@news.boeing.com...
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
Joe Laughlin wrote:


These weird errors just keep on coming! Thanks everyone again for your
help.

Please don't tell me this error is because of const again... :-( But I'd
wager it is.
// 6-9 from Accelerated C++
// Use library algorithm to cat all elements of a vector<string>

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

int main()
{
std::vector<std::string> vec;
vec.push_back("Hi!");
vec.push_back(" How ");
vec.push_back("are ");
vec.push_back("you ");
vec.push_back("doing?");

std::string new_string;

copy(vec.begin(), vec.end(), std::back_inserter(new_string));

std::cout << new_string << std::endl;

return 0;
}
g++ -Wall -pedantic -o 6-9 6-9.cpp
/usr/include/c++/3.2.2/bits/stl_algobase.h: In function `_OutputIter
std::__copy(_RandomAccessIter, _RandomAccessIter, _OutputIter,
std::random_access_iterator_tag) [with _RandomAccessIter = std::string*,
_OutputIter = std::back_insert_iterator<std::string>]':
/usr/include/c++/3.2.2/bits/stl_algobase.h:260: instantiated from
`_OutputIter std::__copy_aux2(_InputIter, _InputIter, _
OutputIter, __false_type) [with _InputIter = std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:303: instantiated from
`_OutputIter std::__copy_ni2(_InputIter, _InputIter, _O
utputIter, __false_type) [with _InputIter = std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:314: instantiated from
`_OutputIter std::__copy_ni1(_InputIter, _InputIter, _O
utputIter, __true_type) [with _InputIter =
__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string,
std::allocat
or<std::string> > >, _OutputIter = std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:349: instantiated from
`_OutputIter std::copy(_InputIter, _InputIter, _OutputI
ter) [with _InputIter = __gnu_cxx::__normal_iterator<std::string*,
std::vector<std::string, std::allocator<std::string> > >
, _OutputIter = std::back_insert_iterator<std::string>]'
6-9.cpp:22: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algobase.h:241: no match for `
std::back_insert_iterator<std::string>& = std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&' operator
/usr/include/c++/3.2.2/bits/stl_iterator.h:355: candidates are:
std::back_insert_iterator<_Container>&

std::back_insert_iterator<_Container>::operator=(_ Container::const_reference
)
[with _Container = std::string]
/usr/include/c++/3.2.2/bits/stl_iterator.h:330:
std::back_insert_iterator<std::string>&
std::back_insert_iterator<std::string>::operator=( const
std::back_insert_iterator<std::string>&)
Jul 22 '05 #11

"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:I6********@news.boeing.com...
Joe Laughlin wrote:

These weird errors just keep on coming! Thanks everyone again for your
help.

Please don't tell me this error is because of const again... :-( But I'd
wager it is.


Not this time. :-)
See below.
// 6-9 from Accelerated C++
// Use library algorithm to cat all elements of a vector<string>

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

int main()
{
std::vector<std::string> vec;
vec.push_back("Hi!");
vec.push_back(" How ");
vec.push_back("are ");
vec.push_back("you ");
vec.push_back("doing?");

std::string new_string;

copy(vec.begin(), vec.end(), std::back_inserter(new_string));

std::cout << new_string << std::endl;

return 0;
}
g++ -Wall -pedantic -o 6-9 6-9.cpp
/usr/include/c++/3.2.2/bits/stl_algobase.h: In function `_OutputIter
std::__copy(_RandomAccessIter, _RandomAccessIter, _OutputIter,
std::random_access_iterator_tag) [with _RandomAccessIter = std::string*, _OutputIter = std::back_insert_iterator<std::string>]':
/usr/include/c++/3.2.2/bits/stl_algobase.h:260: instantiated from
`_OutputIter std::__copy_aux2(_InputIter, _InputIter, _
OutputIter, __false_type) [with _InputIter = std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:303: instantiated from
`_OutputIter std::__copy_ni2(_InputIter, _InputIter, _O
utputIter, __false_type) [with _InputIter = std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:314: instantiated from
`_OutputIter std::__copy_ni1(_InputIter, _InputIter, _O
utputIter, __true_type) [with _InputIter =
__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string,
std::allocat
or<std::string> > >, _OutputIter = std::back_insert_iterator<std::string>]' /usr/include/c++/3.2.2/bits/stl_algobase.h:349: instantiated from
`_OutputIter std::copy(_InputIter, _InputIter, _OutputI
ter) [with _InputIter = __gnu_cxx::__normal_iterator<std::string*,
std::vector<std::string, std::allocator<std::string> > >
, _OutputIter = std::back_insert_iterator<std::string>]'
6-9.cpp:22: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algobase.h:241: no match for `
std::back_insert_iterator<std::string>& = std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&' operator
/usr/include/c++/3.2.2/bits/stl_iterator.h:355: candidates are:
std::back_insert_iterator<_Container>&

std::back_insert_iterator<_Container>::operator=(_ Container::const_reference )
[with _Container = std::string]
/usr/include/c++/3.2.2/bits/stl_iterator.h:330:
std::back_insert_iterator<std::string>&
std::back_insert_iterator<std::string>::operator=( const
std::back_insert_iterator<std::string>&)


'back_insert_iterator' requires that the container type specified by
its template argument defines members 'push_back()' and 'value_type'.
'std::string' defines the latter, but not the former.

IOW you cannot use 'back_insert_iterator' with a 'std::string'.
('std::string', while sharing much commonality with them, isn't
strictly a 'container').

-Mike
Jul 22 '05 #12
Mike Wahler wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in
message news:I6********@news.boeing.com...
Joe Laughlin wrote:


These weird errors just keep on coming! Thanks everyone
again for your help.

Please don't tell me this error is because of const
again... :-( But I'd wager it is.


Not this time. :-)
See below.
// 6-9 from Accelerated C++
// Use library algorithm to cat all elements of a
vector<string>

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

int main()
{
std::vector<std::string> vec;
vec.push_back("Hi!");
vec.push_back(" How ");
vec.push_back("are ");
vec.push_back("you ");
vec.push_back("doing?");

std::string new_string;

copy(vec.begin(), vec.end(),
std::back_inserter(new_string));

std::cout << new_string << std::endl;

return 0;
}
g++ -Wall -pedantic -o 6-9 6-9.cpp
/usr/include/c++/3.2.2/bits/stl_algobase.h: In function
`_OutputIter std::__copy(_RandomAccessIter,
_RandomAccessIter, _OutputIter,
std::random_access_iterator_tag) [with
_RandomAccessIter = std::string*, _OutputIter =
std::back_insert_iterator<std::string>]':
/usr/include/c++/3.2.2/bits/stl_algobase.h:260:
instantiated from `_OutputIter
std::__copy_aux2(_InputIter, _InputIter, _
OutputIter, __false_type) [with _InputIter =
std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:303:
instantiated from `_OutputIter
std::__copy_ni2(_InputIter, _InputIter, _O
utputIter, __false_type) [with _InputIter =
std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:314:
instantiated from `_OutputIter
std::__copy_ni1(_InputIter, _InputIter, _O
utputIter, __true_type) [with _InputIter =
__gnu_cxx::__normal_iterator<std::string*,
std::vector<std::string, std::allocat
or<std::string> > >, _OutputIter =

std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:349:
instantiated from `_OutputIter std::copy(_InputIter,
_InputIter, _OutputI
ter) [with _InputIter =
__gnu_cxx::__normal_iterator<std::string*,
std::vector<std::string, std::allocator<std::string> > >
, _OutputIter = std::back_insert_iterator<std::string>]'
6-9.cpp:22: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algobase.h:241: no
match for ` std::back_insert_iterator<std::string>& =
std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&' operator
/usr/include/c++/3.2.2/bits/stl_iterator.h:355:
candidates are: std::back_insert_iterator<_Container>&

std::back_insert_iterator<_Container>::operator=(_ Container::const_reference
)
[with _Container = std::string]
/usr/include/c++/3.2.2/bits/stl_iterator.h:330:
std::back_insert_iterator<std::string>&
std::back_insert_iterator<std::string>::operator=( const
std::back_insert_iterator<std::string>&)


'back_insert_iterator' requires that the container type
specified by its template argument defines members
'push_back()' and 'value_type'. 'std::string' defines the
latter, but not the former.

IOW you cannot use 'back_insert_iterator' with a
'std::string'. ('std::string', while sharing much
commonality with them, isn't strictly a 'container').

-Mike


Argh.

From "Accelerated C++", pg 121:

"back_inserter(c)
Yields an iterator on the container c that appends elements to c. The
container must support push_back, which the list, vector, and the string
types all do."
Jul 22 '05 #13
Joe Laughlin wrote:
Mike Wahler wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in
message news:I6********@news.boeing.com...
Joe Laughlin wrote:
These weird errors just keep on coming! Thanks everyone
again for your help.

Please don't tell me this error is because of const
again... :-( But I'd wager it is.


Not this time. :-)
See below.
// 6-9 from Accelerated C++
// Use library algorithm to cat all elements of a
vector<string>

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

int main()
{
std::vector<std::string> vec;
vec.push_back("Hi!");
vec.push_back(" How ");
vec.push_back("are ");
vec.push_back("you ");
vec.push_back("doing?");

std::string new_string;

copy(vec.begin(), vec.end(),
std::back_inserter(new_string));

std::cout << new_string << std::endl;

return 0;
}
g++ -Wall -pedantic -o 6-9 6-9.cpp
/usr/include/c++/3.2.2/bits/stl_algobase.h: In function
`_OutputIter std::__copy(_RandomAccessIter,
_RandomAccessIter, _OutputIter,
std::random_access_iterator_tag) [with
_RandomAccessIter = std::string*, _OutputIter =
std::back_insert_iterator<std::string>]':
/usr/include/c++/3.2.2/bits/stl_algobase.h:260:
instantiated from `_OutputIter
std::__copy_aux2(_InputIter, _InputIter, _
OutputIter, __false_type) [with _InputIter =
std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:303:
instantiated from `_OutputIter
std::__copy_ni2(_InputIter, _InputIter, _O
utputIter, __false_type) [with _InputIter =
std::string*, _OutputIter =
std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:314:
instantiated from `_OutputIter
std::__copy_ni1(_InputIter, _InputIter, _O
utputIter, __true_type) [with _InputIter =
__gnu_cxx::__normal_iterator<std::string*,
std::vector<std::string, std::allocat
or<std::string> > >, _OutputIter =

std::back_insert_iterator<std::string>]'
/usr/include/c++/3.2.2/bits/stl_algobase.h:349:
instantiated from `_OutputIter std::copy(_InputIter,
_InputIter, _OutputI
ter) [with _InputIter =
__gnu_cxx::__normal_iterator<std::string*,
std::vector<std::string, std::allocator<std::string> > >
, _OutputIter = std::back_insert_iterator<std::string>]'
6-9.cpp:22: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algobase.h:241: no
match for ` std::back_insert_iterator<std::string>& =
std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&' operator
/usr/include/c++/3.2.2/bits/stl_iterator.h:355:
candidates are: std::back_insert_iterator<_Container>&

std::back_insert_iterator<_Container>::operator=(_ Container::const_reference
)
[with _Container = std::string]
/usr/include/c++/3.2.2/bits/stl_iterator.h:330:
std::back_insert_iterator<std::string>&
std::back_insert_iterator<std::string>::operator=( const
std::back_insert_iterator<std::string>&)


'back_insert_iterator' requires that the container type
specified by its template argument defines members
'push_back()' and 'value_type'. 'std::string' defines the
latter, but not the former.

IOW you cannot use 'back_insert_iterator' with a
'std::string'. ('std::string', while sharing much
commonality with them, isn't strictly a 'container').

-Mike


Argh.

From "Accelerated C++", pg 121:

"back_inserter(c)
Yields an iterator on the container c that appends
elements to c. The container must support push_back,
which the list, vector, and the string types all do."


So, was the book correct? Or wrong?
Jul 22 '05 #14
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:I6********@news.boeing.com...
Joe Laughlin wrote:

From "Accelerated C++", pg 121:

"back_inserter(c)
Yields an iterator on the container c that appends
elements to c. The container must support push_back,
which the list, vector, and the string types all do."


So, was the book correct? Or wrong?


I think it's wrong. As far as I can tell from the standard,
'std::basic_string<>' is not required to provide 'push_back()',
but neither is it prohibited from doing so.

Of course I invite anyone else to give their interpretation,
so we can know for sure.

I scanned the AC++ errata pages, but I don't see this issue
mentioned.

Anyone else?

-Mike
Jul 22 '05 #15
Mike Wahler wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:I6********@news.boeing.com...
Joe Laughlin wrote:
From "Accelerated C++", pg 121:

"back_inserter(c)
Yields an iterator on the container c that appends
elements to c. The container must support push_back,
which the list, vector, and the string types all do."
So, was the book correct? Or wrong?

I think it's wrong. As far as I can tell from the standard,
'std::basic_string<>' is not required to provide 'push_back()',
but neither is it prohibited from doing so.


ISO/IEC 14882 1998, p385

// 21.3.5: modifiers
....
void push_back (charT);

Of course I invite anyone else to give their interpretation,
so we can know for sure.

I scanned the AC++ errata pages, but I don't see this issue
mentioned.

Anyone else?

Jul 22 '05 #16

"Buster" <no**@none.com> wrote in message
news:cl**********@newsg2.svr.pol.co.uk...
Mike Wahler wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:I6********@news.boeing.com... I think it's wrong. As far as I can tell from the standard,
'std::basic_string<>' is not required to provide 'push_back()',
but neither is it prohibited from doing so.


ISO/IEC 14882 1998, p385

// 21.3.5: modifiers
...
void push_back (charT);


But that's the *only* mention of it. For all the other
member functions, there is a description of behavior.
Not for this one. ???

-Mike
Jul 22 '05 #17
Mike Wahler wrote in news:6fRfd.4096$kM.1575
@newsread3.news.pas.earthlink.net in comp.lang.c++:

"Buster" <no**@none.com> wrote in message
news:cl**********@newsg2.svr.pol.co.uk...
Mike Wahler wrote:
> "Joe Laughlin" <Jo***************@boeing.com> wrote in message
> news:I6********@news.boeing.com... > I think it's wrong. As far as I can tell from the standard,
> 'std::basic_string<>' is not required to provide 'push_back()',
> but neither is it prohibited from doing so.


ISO/IEC 14882 1998, p385

// 21.3.5: modifiers
...
void push_back (charT);


But that's the *only* mention of it. For all the other
member functions, there is a description of behavior.
Not for this one. ???


21.3/2

The class template basic_string conforms to the requirements of a
Sequence, as specified in (23.1.1). Additionally, because the
iterators supported by basic_string are random access iterators
(24.1.5), basic_string conforms to the the requirements of a
Reversible Container, as specified in (23.1).

Table 68 - Optional Sequence operations (23.1.1/12) describes push_back.
Presunmably its only because its optional that its mentioned in 21.3.5
at all.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #18
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<6f****************@newsread3.news.pas.earthl ink.net>...

[ ... ]
// 21.3.5: modifiers
...
void push_back (charT);


But that's the *only* mention of it. For all the other
member functions, there is a description of behavior.
Not for this one. ???


Theoretically, it's handled by ($21.3/2):

The template class basic_string conforms to the requirements
of a Sequence, as specified in (23.1.1).

Unfortunately, 23.1.1/12 also only mentions the existence of
push_back, and leaves describing it in any detail for the individual
container classes that provide it -- but (of course) not including
string...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #19

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

Similar topics

3
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...
5
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...
1
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...
2
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...
6
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...
0
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...
0
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...
7
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...
6
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...
4
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.