I'm trying to write a proof-of-concept basic string parser in C++, something I had no trouble with in other languages. C++ is giving me a headache, however, because it uses pointers in place of Passing By Reference, and the syntax is quite confusing.
My parsing function:
1 2 3 4 5 6 7 8 9 10 11 12 13
string Parse_String(string *Input, string Delimiter1, string Delimiter2) // Parses input for anything between "Delimiter1" and "Delimiter2"
{
int Position = -1; //The current character being checked/appended
string Found_String; //The string found to be within the delimiters
for (Position = (*Input).find(Delimiter1) + 1; Position < (*Input).length(); Position++) // Starts at the character right after the first delimiter, and loops through the rest of the string
{
if ((*Input)[Position] == Delimiter2) //syntax error #1
return Found_String;
else
Found_String.append((*Input)[Position]); //syntax error #2
}
}
At error #1, I'm trying to check if the program has reached the second delimiter, and if so return what it has already appended to Found_String.
Error #2 is where I'm actually appending the current character if it's been determined it hasn't yet reached the second delimiter.
I can make the program function perfectly by using a non-pointer version of Input, but then I would be unable to modify the source.
if ((*Input)[Position] == Delimiter2)
Error: no operator "==" matches these operands
Found_String.append((*Input)[Position]);
Error: no instance of overloaded function "std::basic_string<_Elem, _Traits, _Ax>::appendwith _Elem=char, _Traits=std::char_traits<char>, _Ax=std:allocator<char>" matches the argument list
Both of these errors appear when I try to use the pointer rather than a local string variable.
Just use a reference then.
Do you mean that C++ supports pass-by-reference? I was told that pointers were needed to accomplish that.
For the first error, you're trying to compare a single character to a string. I'm guessing what you mean to do is Input->find(Delimiter2)==0.
Is Delimiter2 is supposed to be a single character? If so, it'd be better to pass it as a char.
For the second error, std::string::append takes a string, not a character.
You either mean to append a single character to the string: Found_String.push_back((*Input)[Position]);
or append a whole portion of the string: Found_String.append(Input->begin()+Position,Input->end());