I have a recursive method that traverses a linked list for a first name or last name then deletes the node from the list. This is what I have:

void game::DeleteR(string first_name, string last_name, ListNode* &node)
//throw (ContestantException)
{
	/* See if we are at end of list. */
	if (node == NULL)
	//throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: insert index    out of range");  need to change this message

	else if (node->item == first_name) || (node->item == last_name))
	{


		/*
		* Check to see if current node is one
		* to be deleted.
		*/
		if ((node->item == first_name) || (node->item == last_name))
		{
			ListNode *tempNextP;

			/* Save the next pointer in the node. */
			tempNextP = node->next;

			/* Deallocate the node. */
			free(node);

			/*
			 * Return the NEW pointer to where we
			 * were called from.  I.e., the pointer
			 * the previous call will use to "skip
			 * over" the removed node.
			 */
			node == tempNextP;
		}
	}
	
	else DeleteR(first_name, last_name, node->next);
}

At line 16 I het a compile error " no match for 'operator==' in 'node->game::ListNode::item == first_name'". Can I not use a comparison operator?

Recommended Answers

All 8 Replies

my GUESS is that item and first_name are character arrays. If that is right then you have to call strcmp() to check if the two strings are the same. Or you can change them to std::string.

No character arrays. item is defined by typedef as an instance to another class. first_name and last_name are C++ strings.

You can't compare directly (without operator overload) an instance of another clas (node->item) with c++ strings.

Couldn't I compare by changing the statement to:

node->item->data member of the other class

if dataMember is a string variable encapsulated in the object item, then yes, you could potentially use either node->item.dataMember or node->item->dataMember depending on whether item is on object per se' or a pointer to type.

Ok, I cleaned up my code a bit from the previous post:

void game::DeleteContestantR(std::string first_name, std::string last_name, ListNode* &node)
//throw (ContestantException)
{
	/* See if we are at end of list. */
	if (node == NULL);
	//throw ListIndexOutOfRangeException("ListIndexOutOfRangeException: insert index out of range");

	else if ((node->item->firstName == first_name) && (node->item->lastName == last_name))
	{
		ListNode *tempNode = node;
		node = node->next;
		delete tempNode;
	}
	else DeleteContestantR(first_name, last_name, node->next);
}

Now I get a compile error that firstName and lastName is private. Which they are private in my contestant class. I thought I am able to point to them because when I defined the ListItemType for my linked list, I used a pointer to the Contestant class:

typedef Contestant* ListItemType;

If they are private you can only access them with member functions of the class to which both variable and function belong or by using a friend.

Great, it worked! Thanks to everyone for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.