I am running into some unexpected string behavior when I append an Iterator to a string. Basically, I have a file that reads something along the lines of:
int x := 10;
print x;
I have a string already that contains the contents of this file, and I am iterating through it and simply removing whitespace right now.
void Lexer::parse()
{
Pointer = Filestring.begin(); // both private members
while(Pointer < Filestring.end())
{
if(is_whitespace(0)) // 0 indicates current Pointer position
{
advance(Pointer, 1);
continue;
}
CurToken.append(&*Pointer); // CurToken is private member string
advance(Pointer, 1);
}
cout << CurToken << endl;
}
By the end of the loop I would expect CurToken to be a string that contains nothing but the characters with all whitespace removed. Instead I get something like the following:
int x := 1 + 2;
print x;
nt x := 1 + 2;
print x;
t x := 1 + 2;
print x;
…
rint x;
int x;
nt x;
t x;
x;
;
I assume the issue is de-referencing the pointer, and if so, how can I append the current pointer position’s character? If I do not de-refrence it, I end up with invalid conversion from ‘char’ to ‘const char*’
Note: is_whitespace() I have tested, and works as expected. So that can be ruled out as a possible problem.
What are the types of
CurTokenandPointer?CurTokenlooks like astd::string. IsPointerastd::string*?appendhas quite a few overloads. One of them is:If you want to hit that one you should get rid of the ‘&’ to give you just the dereferenced pointer:
That should work, but remember that it’ll append the whole string not just the character. If it doesn’t you probably need to figure out which overload of
append()it’s hitting. If you want the first character only, try: