By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

string lengths as integers.

P: n/a
I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

However the compiler (the Visual C++ one) returns the following error:
'substr' : cannot convert parameter 1 from 'unsigned int
(void) const' to 'unsigned int' Conversion is a valid
standard conversion, which can be performed implicitly or by
use of static_cast, C-style cast or function-style cast

I tried static_casting, C-Style and function-style to assign an int
variable the value of the string length, but I got the exact same
error every time.
I also tried use strlen(pStr) and got a similar error (only the data
type of the original was different).

Is there any way to make this work?
Any help is greatly appreciated!

-==Kensu==-
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Chris Schumacher" <ke*****@hotmail.com> wrote in message
news:kn********************************@4ax.com...
I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

1) Did you mean:
pStr.length()
?
std::string::length is a member function, not a data member.

2) in C++, char is not equivalent to a 1-character string.
To get the n-th character of a string, you need to
use the [] operator instead of substr.
And character indexes are 0-based, so the last character
is at position length()-1 :
char b = pStr[ pStr.length()-1 ];
hth
--
http://ivan.vecerina.com
Jul 19 '05 #2

P: n/a

Length is a function, not a scalar.
Try pStr.substr(pStr.length(), 1)

--
Andy Kashyap
to reply, sed -e 's/nospam//g' emailAddress

"Chris Schumacher" <ke*****@hotmail.com> wrote in message
news:kn********************************@4ax.com...
I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

However the compiler (the Visual C++ one) returns the following error:
'substr' : cannot convert parameter 1 from 'unsigned int
(void) const' to 'unsigned int' Conversion is a valid
standard conversion, which can be performed implicitly or by
use of static_cast, C-style cast or function-style cast

I tried static_casting, C-Style and function-style to assign an int
variable the value of the string length, but I got the exact same
error every time.
I also tried use strlen(pStr) and got a similar error (only the data
type of the original was different).

Is there any way to make this work?
Any help is greatly appreciated!

-==Kensu==-

Jul 19 '05 #3

P: n/a
Try
char b = *pstr.rbegin();
Catalin

"Chris Schumacher" <ke*****@hotmail.com> wrote in message
news:kn********************************@4ax.com...
I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

However the compiler (the Visual C++ one) returns the following error:
'substr' : cannot convert parameter 1 from 'unsigned int
(void) const' to 'unsigned int' Conversion is a valid
standard conversion, which can be performed implicitly or by
use of static_cast, C-style cast or function-style cast

I tried static_casting, C-Style and function-style to assign an int
variable the value of the string length, but I got the exact same
error every time.
I also tried use strlen(pStr) and got a similar error (only the data
type of the original was different).

Is there any way to make this work?
Any help is greatly appreciated!

-==Kensu==-

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.