I have a new function called removeInelligibleCharsFromTargetName.
void removeInelligibleCharsFromTargetName(string *targetName)
{
for(int i = 0; i < targetName->length(); i++)
{
for(int j = 0; j < ineligibleChars.length(); j++)
{
if(targetName[i] == ineligibleChars[j])
targetName[i] = '_';
}
}
}
The problem is when I try the comparison in the if loop I get the following error:
error C2678: binary ‘==’ : no operator found which takes a left-hand operand of type ‘std::string’ (or there is no acceptable conversion)
32> c:\program files\microsoft sdks\windows\v6.0a\include\guiddef.h(192): could be ‘int operator ==(const GUID &,const GUID &)’ while trying to match the argument list ‘(std::string, char)’
But put that exact same nested loop back where I call it instead of calling the function it works fine.
Can someone tell me whey it won’t work in a function but works fine outside the function. No doubt its something about it being a pointer but I dont know what.
Why are you passing a pointer to a
string? That’s a really bad idea. Pass a reference.The problem is that when you have a pointer,
targetName[i]is the same as*(targetName+i). That is the equivalent of indexing into an array of strings. If you don’t have an array of strings this would only lead to undefined behaviour. You were lucky that the code doesn’t compile (can’t compare a string with a character) and the compiler caught the error. If it happened to compile, when you ran it you would probably observe some very strange behaviour.When you have a
string, or a reference to one,targetName[i]invokes theoperator[]on the string, which indexes into the string and actually gives you a character.