Erasing a Space in a String

I am trying to erase a space in a string.
First Name is a private member variable so that is why I used an accessor function. Is something wrong with the following call? I keep on getting errors...

add.getFirstName().erase(add.getFirstName().begin, 1);
If getFirstName() returns a copy, you're only erasing it from the copy - you need to store the copy erase the space, and then setFirstName() to the modified string.
closed account (3qX21hU5)
Could you post the code for your class?

If getFirstName() returns a copy, you're only erasing it from the copy - you need to store the copy erase the space, and then setFirstName() to the modified string.


Well if the OP is calling the function correctly, getFirstName() would return a reference to itself and then call erase so I don't believe it is returning a copy.

But this is all speculation without actually seeing the class's code.
Zereo wrote:
Well if the OP is calling the function correctly, getFirstName() would return a reference to itself and then call erase so I don't believe it is returning a copy.
You can always call non-const member functions on non-const rvalues. It is most likely that the OPer's getFirstName() returns a copy.
Last edited on
closed account (3qX21hU5)
Ohh ok I see what you are saying now didn't really understand the OP at first sorry about that.

But we will still need a bit more information and the code to be able to tell you exactly what is going wrong. But LB is right you are most likely trying to alter a copy instead of the actual thing.


Topic archived. No new replies allowed.