|
-
April 6th, 2009, 01:10 PM
#1
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.
-
April 6th, 2009, 01:12 PM
#2
Re: newbster question Name.size
Why are you dereferencing it? What error are you getting?
-
April 6th, 2009, 01:17 PM
#3
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.
-
April 6th, 2009, 01:17 PM
#4
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.
-
April 6th, 2009, 01:39 PM
#5
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.
-
April 6th, 2009, 01:41 PM
#6
Re: newbster question Name.size
 Originally Posted by ArmlessBastard
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.
-
April 6th, 2009, 01:50 PM
#7
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.
-
April 6th, 2009, 02:03 PM
#8
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
-
April 6th, 2009, 02:09 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|