string lengths as integers.

Discussion in 'C++' started by Chris Schumacher, Nov 14, 2003.

  1. 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==-
    Chris Schumacher, Nov 14, 2003
    #1
    1. Advertising

  2. "Chris Schumacher" <> wrote in message
    news:...
    > 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
    Ivan Vecerina, Nov 14, 2003
    #2
    1. Advertising

  3. Chris Schumacher

    Andy Kashyap Guest

    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" <> wrote in message
    news:...
    > 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==-
    Andy Kashyap, Nov 14, 2003
    #3
  4. Try
    char b = *pstr.rbegin();


    Catalin

    "Chris Schumacher" <> wrote in message
    news:...
    > 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==-
    Catalin Pitis, Nov 14, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Taras_96

    Variable 'variable lengths'

    Taras_96, May 8, 2005, in forum: VHDL
    Replies:
    2
    Views:
    491
    Taras_96
    May 9, 2005
  2. Griff

    obtaining lengths of slices

    Griff, Aug 4, 2004, in forum: Perl
    Replies:
    6
    Views:
    416
    Gunnar Hjalmarsson
    Aug 6, 2004
  3. Grumfish
    Replies:
    2
    Views:
    360
    Bengt Richter
    Aug 19, 2003
  4. jt
    Replies:
    3
    Views:
    912
    Keith Thompson
    May 23, 2005
  5. Hal Vaughan

    Maximum Line and String Lengths

    Hal Vaughan, Jan 19, 2004, in forum: Javascript
    Replies:
    8
    Views:
    142
    Hal Vaughan
    Jan 19, 2004
Loading...

Share This Page