Other answers have solved the particular issue that you have, but you should be aware that there are different approaches to solve your actual problem: erase elements that fullfill a condition. That can be easily solved with the remove/erase idiom:
// C++0x enabled compiler
str.erase(
std::remove_if( str.begin(), str.end(),
[](char ch) { return !isalpha(ch) && !isdigit(ch) && ch != '-' } ),
str.end() );
While this might look cumbersome at first, once you have seen it a couple of times it will no longer be surprising, and it is an effective way of deleting elements from a vector or string.
If your compiler does not have lambda support, then you can create a functor and pass it as the third argument to remove_if:
// at namespace level, sadly c++03 does not allow you to use local classes in templates
struct mycondition {
bool operator()( char ch ) const {
return !isalpha(ch) && !isdigit(ch) && ch != '-';
}
};
// call:
str.erase(
std::remove_if( str.begin(), str.end(), mycondition() ),
str.end() );