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?
Re: newbster question Name.size
Why are you dereferencing it? What error are you getting?
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.
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.
Re: newbster question Name.size
Quote:
-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.
Re: newbster question Name.size
Quote:
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.
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.
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
Re: newbster question Name.size
Ah, I tried putting () on both reverse and name. Thanks for the help.