CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    13

    newbster question Name.size

    Trying to use Name.size() to find out how many characters the user puts in. Name is a string::std can I not use .size for this? how can I accomplish this feat.

    Code:
    for (i=*Name.size(); i>0; --i)
    	{
    		//need to bring this to main()	
    		*Reverse=*Reverse+*Name[i];
    	}
    Error
    Code:
    c:\documents and settings\administrator\my documents\visual studio 2005\projects\reverseit\reverseit\function.cpp(21) : error C2228: left of '.size' must have class/struct/union
            type is 'std::string *'
            did you intend to use '->' instead?
    what is the '->' that the error is mentioned?
    Last edited by ArmlessBastard; April 6th, 2009 at 01:12 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: newbster question Name.size

    Why are you dereferencing it? What error are you getting?

  3. #3
    Join Date
    Jul 2005
    Posts
    266

    Re: newbster question Name.size

    if you have a pointer to a std:string

    e.g. string * Name

    you can either use (*Name).size() or Name->size() to call the function, provided you have already initialized that pointer with a new string object.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: newbster question Name.size

    Two things wrong with that code:

    1) The * operator's precedence is such that it thinks you're trying to dereference the result of size(), which you aren't. You can do
    Code:
    (*Name).size()
    // or, equivalently
    Name->size()
    Notice this latter is right there in the error message.

    2) You aren't looping over right indexes. In particular,
    Code:
    (*Name)[Name->size()]
    will always be invalid, because the largest valid index is Name->size() - 1.

    Those are the only really *wrong* things in the code. However, consider:
    -In many cases it's better to pass by reference rather than passing a pointer, because the syntax is much easier to deal with.
    -It will be faster to pre-size Reverse to the same size as Name, and then just assign directly to the indexes, rather than using operator+=.
    -The std::reverse function already exists.

  5. #5
    Join Date
    Mar 2009
    Posts
    13

    Re: newbster question Name.size

    -The std::reverse function already exists.
    I also have an error with in the function I am using, what is this reverse function that exists already?

    The error is illegal indirection for the function in the loop listed above.

    Also using the instructions above worked but I got a warning for converting the string_t into int should I change that variable, I really don't see how it could effect the program.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: newbster question Name.size

    Quote Originally Posted by ArmlessBastard View Post
    I also have an error with in the function I am using, what is this reverse function that exists already?

    The error is illegal indirection for the function in the loop listed above.
    It's what everybody's been telling you. Implement the changes suggested and try again.

  7. #7
    Join Date
    Mar 2009
    Posts
    13

    Re: newbster question Name.size

    @GCDEF
    I did what you guys said. It fixed the first error the next error happens a few lines below

    Code:
    *Reverse=*Reverse+*Name[i];
    The error happens on this line and says that there is illegal indirection.

  8. #8
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: newbster question Name.size

    Don't know what Reverse is. Let's assume its a pointer to string as well .
    Then it would be

    Code:
    *Reverse=*Reverse+(*Name)[i];
    or simpler
    Code:
    *Reverse+=(*Name)[i];
    Kurt

  9. #9
    Join Date
    Mar 2009
    Posts
    13

    Re: newbster question Name.size

    Ah, I tried putting () on both reverse and name. Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured