473,487 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why is this code complaining about constness?

I'm using visual studio 8, and the following code is failing. For the
life of me, I can't see what's wrong...

void myTest(std::string const& in)
{
using namespace std;
string::const_iterator lastNonWhitespace = find_if(in.rbegin(),
in.rend(), not1(ptr_fun(isspace)));
}

The error is:
strutils.cpp|226 error 2440| 'initializing' : cannot convert from
'std::reverse_iterator<_RanIt>' to
'std::_String_const_iterator<_Elem,_Traits,_Alloc> '
|| with
|| [
||
_RanIt=std::_String_const_iterator<char,std::char_ traits<char>,std::allocator<char>>
|| ]
|| and
|| [
|| _Elem=char,
|| _Traits=std::char_traits<char>,
|| _Alloc=std::allocator<char>
|| ]
|| No constructor could take the source type, or constructor
overload resolution was ambiguous

Aug 1 '07 #1
7 1999
markscottwright wrote:
I'm using visual studio 8, and the following code is failing. For the
life of me, I can't see what's wrong...

void myTest(std::string const& in)
{
using namespace std;
string::const_iterator lastNonWhitespace = find_if(in.rbegin(),
Did you mean to declare it 'string::const_reverse_iterator'? If
not, you might want to look into the 'base' member, as in

string::const_iterator lastNonWhitspace = find_if(...) . base();
in.rend(), not1(ptr_fun(isspace)));
}

The error is:
strutils.cpp|226 error 2440| 'initializing' : cannot convert from
'std::reverse_iterator<_RanIt>' to
'std::_String_const_iterator<_Elem,_Traits,_Alloc> '
>> with
[
_RanIt=std::_String_const_iterator<char,std::char_ traits<char>,std::allocator<char>>
>> ]
and
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
No constructor could take the source type, or constructor
overload resolution was ambiguous
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '07 #2
markscottwright wrote:
:: I'm using visual studio 8, and the following code is failing. For
:: the life of me, I can't see what's wrong...
::
:: void myTest(std::string const& in)
:: {
:: using namespace std;
:: string::const_iterator lastNonWhitespace = find_if(in.rbegin(),
:: in.rend(), not1(ptr_fun(isspace)));
:: }
::
:: The error is:
:: strutils.cpp|226 error 2440| 'initializing' : cannot convert from
:: 'std::reverse_iterator<_RanIt>' to
:: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> '

It says it all right here -- rbegin() returns a reverse_iterator (and
so will find_if), which cannot be assigned to a const_iterator. They
are just different types, with no conversion defined.

Have you looked into std::string's member functions find_first_of,
find_last_not_of, etc?
Bo Persson
Aug 1 '07 #3
On Aug 1, 1:36 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
markscottwright wrote:
I'm using visual studio 8, and the following code is failing. For the
life of me, I can't see what's wrong...
void myTest(std::string const& in)
{
using namespace std;
string::const_iterator lastNonWhitespace = find_if(in.rbegin(),

Did you mean to declare it 'string::const_reverse_iterator'? If
not, you might want to look into the 'base' member, as in

string::const_iterator lastNonWhitspace = find_if(...) . base();
in.rend(), not1(ptr_fun(isspace)));
}
The error is:
strutils.cpp|226 error 2440| 'initializing' : cannot convert from
'std::reverse_iterator<_RanIt>' to
'std::_String_const_iterator<_Elem,_Traits,_Alloc> '
> with
[
_RanIt=std::_String_const_iterator<char,std::char_ traits<char>,std::allocator<char>>
> ]
and
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
No constructor could take the source type, or constructor
overload resolution was ambiguous

--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks - that was indeed my problem. I needed to take the resulting
reverse_iterator and get its iterator through the base() method.
Thanks for the help.

Mark

Aug 1 '07 #4
On Aug 1, 8:10 pm, markscottwright <markscottwri...@gmail.comwrote:
I'm using visual studio 8, and the following code is failing. For the
life of me, I can't see what's wrong...
void myTest(std::string const& in)
{
using namespace std;
string::const_iterator lastNonWhitespace = find_if(in.rbegin(),
in.rend(), not1(ptr_fun(isspace)));
Victor has pointed out one of the errors. But the last argument
to find_if can't be correct either. First, of course, because
one of the isspace functions is a template, so argument type
deduction fails if it is visible. (Of course, the one that it a
template takes two arguments, so can't be used here. But the
compiler can't know that until it's instantiated the template,
and it can't instantiate the template until it has chosen the
correct isspace.) And secondly, because calling the one
argument version of isspace (from <cctypeor <ctype.h>) with
the char resulting from the referencing of
std::string::const_reverse_iterator is undefined behavior.
}
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 2 '07 #5
On Aug 2, 4:23 am, James Kanze <james.ka...@gmail.comwrote:
On Aug 1, 8:10 pm, markscottwright <markscottwri...@gmail.comwrote:
I'm using visual studio 8, and the following code is failing. For the
life of me, I can't see what's wrong...
void myTest(std::string const& in)
{
using namespace std;
string::const_iterator lastNonWhitespace = find_if(in.rbegin(),
in.rend(), not1(ptr_fun(isspace)));

Victor has pointed out one of the errors. But the last argument
to find_if can't be correct either. First, of course, because
one of the isspace functions is a template, so argument type
deduction fails if it is visible. (Of course, the one that it a
template takes two arguments, so can't be used here. But the
compiler can't know that until it's instantiated the template,
and it can't instantiate the template until it has chosen the
correct isspace.) And secondly, because calling the one
argument version of isspace (from <cctypeor <ctype.h>) with
the char resulting from the referencing of
std::string::const_reverse_iterator is undefined behavior.
}

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
C++. Sigh.

So, what *is* the correct way to find the last non-whitespace
character in a string?
Aug 2 '07 #6

markscottwright <ma*************@gmail.comwrote in message...
>So, what *is* the correct way to find the last non-whitespace
character in a string?
std::string test271("hello, my name is mud! ");
size_t indx( test271.find_last_not_of( ' ' ) );
if( indx != test271.npos ){
cout<<"indx="<<indx<<std::endl;
cout<<"test271.substr(indx)="
<<test271.substr(indx)<<std::endl;
cout<<"test271.at( indx )="
<<test271.at( indx )<<std::endl;
} // if()

/* -out-
indx=21
test271.substr(indx)=! // which is "! ".
test271.at( indx )=!
*/

That just finds a 'space' char.
Look up the other overloads of std::string find*** for more.

http://www.dinkumware.com/manuals/.

--
Bob R
POVrookie
Aug 2 '07 #7
On Aug 2, 9:30 pm, markscottwright <markscottwri...@gmail.comwrote:
On Aug 2, 4:23 am, James Kanze <james.ka...@gmail.comwrote:
On Aug 1, 8:10 pm, markscottwright <markscottwri...@gmail.comwrote:
I'm using visual studio 8, and the following code is failing. For the
life of me, I can't see what's wrong...
void myTest(std::string const& in)
{
using namespace std;
string::const_iterator lastNonWhitespace = find_if(in.rbegin(),
in.rend(), not1(ptr_fun(isspace)));
Victor has pointed out one of the errors. But the last argument
to find_if can't be correct either. First, of course, because
one of the isspace functions is a template, so argument type
deduction fails if it is visible. (Of course, the one that it a
template takes two arguments, so can't be used here. But the
compiler can't know that until it's instantiated the template,
and it can't instantiate the template until it has chosen the
correct isspace.) And secondly, because calling the one
argument version of isspace (from <cctypeor <ctype.h>) with
the char resulting from the referencing of
std::string::const_reverse_iterator is undefined behavior.
}
C++. Sigh.
Don't blame C++ for this one. We just inherited it from C:-).
So, what *is* the correct way to find the last non-whitespace
character in a string?
As usual, there is no one correct way. A lot depends on
context. I do a fair amount of text processing from time to
time, so I've written library classes to wrap the functions in
<locale>, particularly those in the ctype facet. For a one time
use, however, that's a lot more than you'd probably want to
type; if internationalization isn't an issue (or if you're happy
to always use the global locale), then just writting something
like:

struct IsWhite
{
bool operator()( char ch ) const
{
return isspace( static_cast< unsigned char >( ch ) ) ;
}
} ;

and using it as your predicate (possibly with std::not1), should
be largely sufficient.

Officially, of course, you're supposed to use the std::ctype
facet defined in <local>. But everything in <localis pretty
much a horror with regards to ease of use, so unless you really
need full internationalization, something like the above is much
simpler, and just as good. Just remember to cast any char to
unsigned char before invoking any function in
<ctype.h>/<cctype>, and you should be OK.

And also: be wary of passing the address of a function to a
template function (e.g. as predicate or functional object).
Type deduction and function overload resolution don't always
work very well in such cases: basically, type deduction needs to
know the results of function overload resolution, and vice
versa, so the compiler gives up, and says ambiguous. This is
especially true in cases like the above, because the overloaded
functions are found in <locale>, which may or may not be
indirectly included. It's a bit of a pain to have to write
small classes like the above, but it avoids problems in the long
run.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 3 '07 #8

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

Similar topics

0
1095
by: Dave | last post by:
Hi, I just want to share what I have found today. In the online article "Enforce Constness With C#" by Bill Wagner for "Visual Studio Magazine", he explaines how to make a little workaround to...
15
7562
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
13
1280
by: Gernot Frisch | last post by:
char* s=" "; int main() { strcpy(s, "Hello World"); } This program crashes nicely. I think s is a pointer to a string that is stored in the executable memory, so the...
6
1393
by: Bryan | last post by:
I hate it when people reinvent the wheel. This is some legacy code that I believe may be causing some memory issues. I think there may be a problem in the way that the CVector class is...
13
365
by: Kelvin Moss | last post by:
Hi group, In C++ it's undefined behavior if one tries to un-const the constness of a const variable with const_cast<>. I want to know if the same holds good in C too. E.g. const char *s =...
14
2393
by: jagguy | last post by:
this works char *p ; p="xat"; cout << p <<endl;
11
5004
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
83
3847
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
8
4407
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
0
6967
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...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
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,...
1
4874
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...
0
4565
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...
0
3076
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.