Jump to content
  • Advertisement
Sign in to follow this  
Jesbass

string-length Function

This topic is 4703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there. I'm trying to ascertain the length of a std:string, (not a cstring), and the documentation suggests using the string-length function:
Quote:
Returns the number of characters in the string. number string-length(string) Remarks If the argument is omitted, it returns the number of characters in the string-value of the context node. The following function call returns 4: string-length("abcd") The following function call returns 5: string-length("ab cd") If an argument is not of type string, it is first converted to a string and then evaluated.
However, when I add the following piece of code:
Quote:
Int32 iStringLength = string-length(m_strTeamName);
I get the following errors:
Quote:
e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C2065: 'string' : undeclared identifier e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C3861: 'length': identifier not found, even with argument-dependent lookup
When, in an attempt to remove the first error, I try changing the code to:
Quote:
Int32 iStringLength = std::string-length(m_strTeamName);
I get the following errors:
Quote:
e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C2275: 'std::string' : illegal use of this type as an expression e:\Documents and Settings\David & Angela\Desktop\Rugby League Manager\Season.cpp(74): error C3861: 'length': identifier not found, even with argument-dependent lookup
1) Is there something I've missed out, such as an include file? The documentation doesn't seem to indicate any required header files. :/ 2) Alternatively, is there another way to determine the length of a string? (Preferably a simple one, hehe!)

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure what all that string-length stuff is, because in C++, you cannot have a function with an identifier of string-length, because the - is not allowed for names. I'm thinking it's jsut saying to use the string's length function.

What you need is the std::string::size() function, which returns the length of your string or you can use the length() function, std::string::length(). More information here.

Share this post


Link to post
Share on other sites
Ah, fantastic! Thanks, guys - that fixed the problem! :)

Just in case anyone is curious, the correct piece of code is:

Quote:
Int32 iStringLength = m_strTeamName.length();


Thanks again! :)

EDIT: Actually, there was a minor casting warning, but that was fixed by:

Quote:
Int32 iStringLength = static_cast<Int32>(m_strTeamName.length());


:)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!