I have a bool function, contains(), that returns true if anEntry is equal to a value in an array element and false otherwise. Inside this function I created two for loops to check if there are any duplicate values in the array. If so, then I call a remove() function to remove the duplicate. I feel that the algorithm should be correct but I get the error that says conversion loses qualifiers on the line for the function call to remove(). What does this mean and how can I fix it?
'contains' is a const member function, meaning that it promises not to modify the object it's called on.
'remove' isn't const, so it doesn't make that promise, and therefore presumably modifies the object.
In order to ensure the const promise, you aren't allowed to call non-const member functions from a const member function.
It seems strange that a function called 'contains' is removing duplicates. Usually the member function that inserts elements would ensure that it isn't inserting duplicates in the first place if duplicates aren't allowed.
BTW, all those "end ..." comments are totally useless clutter.
Thanks for the clarification about const; that helps!
I do not have a requirement to remove duplicates from contains. That was just my first impression. I am given a function called add() and that one does not have the const outside the parenthesis. I'll see what I can do with that one.
So, I moved my code to the add function and I made a minor change in the second for loop (counter now starts at one). Also, realized that the limit in the loop should be maxItems, and not itemCount.
I tested the add() function before my code and it works. However, after adding the two for loops to find a duplicate, call remove(), it looks like all the elements are being removed. So, I ran it through the debugger, and on the line if (items[count] == items[counter]) the first is showing the correct value in the element, but the latter is always showing quotations '' for a value in the element. How does that makes sense, and why is it displaying a quotation and not the value in the element.
I think I am seeing why. The main function calls add() with a loop. So, whenever inside the add() function there is only one element being filled until the function is called again. Therefore, I am comparing the first element which has been filled to the second element which is still empty.
Due to the nature of this add() function, it looks like I need to find somewhere else to implement code for removing duplicates.