I made an string class based on page 602 from C++: How to Program 6th edition (Mine is not finished yet). I'm trying to implement 2 functions, in special. One is int* find(char) and another is char** split(char). Split does not return a runtime error, but it does nothing and the program seems freezed. Find just gave me a run time error. What am I doing wrong?
friend std::ostream& operator<<(std::ostream& os, string& str)
{
os << str.str_value;
return os;
}
friend std::istream& operator>>(std::istream& is, string& str)
{
is >> str.str_value;
return is;
}
based on my knowledge:friend functions are declared inside the class: friend std::ostream& operator<<(std::ostream& os, string& str);
and implemented outside the class:
In your split function, the variable ret is uninitialised before being used. Also the code does not fully split the string. What happens when more than one occurrence of the delimiting character is found in the string?
Your find function does not work because you are not returning anything in the function. Frankly I'm surprised this function compiled
It isn't entirely clear from the name and definition of the function what semantics you intend split to have. But, I suspect that 'split' should return a container (such as a vector) of strings, and probably shouldn't be a member of the class. I would expect a string to supply the all of the basic functions that 'split' would need to use, and so 'split' would not need to be a member (or friend) of the class.
Ok. I agree, if it is a member, should return the type. But split need to return 2d something, thats the idea. How would I return a string? I'll keep split as char* and substr as char*, because it is like the operator[] access many times (until end).