I am trying to fix, so that range-based for loops are supported with my custom class. I am also trying to search for an employee, but my search gives me wrong results even when strings match?
For a class to work with range-based for loop, the class needs to define:
begin()
end()
operator++() (pre-increment) if needed
operator*() (dereference) if needed
operator->() (pointer reference) if needed
operator !=() if needed
I am noticing that I cannot perform remove. Why is it wrong?
You are trying to delete a field of an array which means undefined behavior. When the order isn't important you can do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void remove (Employee * e)
{
for (Employee * ee = employees; ee != employees + current_total; ++ee)
{
if (ee == e) {
current_total--;
*ee = employees[current_total]; // Note: this overwrites the found element with the last
std::cout << "Employee was successfully deleted" << std::endl;
return; // Note: This prevents writing beyond the end
}
}
std::cout << "Employee was not deleted" << std::endl;
}
It's also a bit strange that you use a pointer for removing.
Actually line 77/82 create memory leaks because on line 39 you just copy the contents of the objects.
to full match or partial match.
For this you may use the find function of the string. See:
True in this case as the type returned by begin() is just a pointer - so doesn't need operator overloading. If a non-pointer if returned then they are needed.