Conversion Loses Qualifiers

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?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
template <class ItemType>
bool ArraySet<ItemType>::contains(const ItemType & anEntry) const
{
	bool isFound = false;
	int curIndex = 0; // Current array index

	ItemType duplicate;
	for (int count = 0; count < itemCount; count++) {

		
		for (int counter = 0; counter < itemCount; counter++) {
			duplicate = items[count];
			if (items[count] == items[counter])
				remove(duplicate);
		}

	}
	while (!isFound && (curIndex < itemCount))
	{
		isFound = (anEntry == items[curIndex]);

		if (!isFound)
			curIndex++; // Increment to next entry
	} // end while

	return isFound;
} // end contains



template<class ItemType>
bool ArraySet<ItemType>::remove(const ItemType & anEntry)
{
	int locatedIndex = getIndexOf(anEntry);
	bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
	if (canRemoveItem)
	{
		itemCount--;
		items[locatedIndex] = items[itemCount];
	}  // end if

	return canRemoveItem;
}  // end remove




template<class ItemType>
int ArraySet<ItemType>::getIndexOf(const ItemType & target) const
{
	bool isFound = false;
	int result = -1;
	int searchIndex = 0;

	// If the bag is empty, itemCount is zero, so loop is skipped
	while (!isFound && (searchIndex < itemCount))
	{
		isFound = (items[searchIndex] == target);
		if (isFound)
		{
			result = searchIndex;
		}
		else
		{
			searchIndex++;
		}  // end if
	}  // end while

	return result;
}  // end getIndexOf 
Last edited on
'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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
template<class ItemType>
bool ArraySet<ItemType>::add(const ItemType& newEntry)
{
	ItemType duplicate;
	bool hasRoomToAdd = (itemCount < maxItems);

	if (hasRoomToAdd)
	{
		items[itemCount] = newEntry;
		
			itemCount++;
		
	}

	for (int count = 0; count < maxItems; count++) {

		for (int counter = 1; counter < maxItems; counter++) {

			duplicate = items[count];
			if (items[count] == items[counter])
				remove(duplicate);
		}
	}
	return hasRoomToAdd;
}  
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.
Topic archived. No new replies allowed.