5 Replies - 384 Views - Last Post: 13 May 2012 - 07:10 PM Rate Topic: -----

#1 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

What is wrong with this function and how to fix it

Posted 13 May 2012 - 06:17 PM

I am working on a program, that does various operations to a sorted list. However there seems to be a problem with one of the functions.
RelationType SortedList::ComparedTo(string otherItem) const
{
	if(str < listData ->info)	
		return LESS;

	else if (otherItem > listData ->info)	
		return GREATER;
	
	else
		return EQUAL;
}


There are no errors here but when I tried to use this function in another function I get an error.
	string SortedList::GetItem(string& item, bool& found)
	{
		bool moreToSearch;
		Node* location;

		location = listData;
		found = false;
		moreToSearch = (location != NULL);

		while (moreToSearch && !found)
		{
			switch(item.ComparedTo(location -> info))
			{
			case GREATER:
				location = location -> next;
				moreToSearch = (location != NULL);
				break;

			case EQUAL:
				found = true;
				item = location -> info;
				break;

			case LESS:
				moreToSearch = false;
				break;
			}//end switch
		}//end while

		return item;
	}


I get a red line under item.ComparedTo that says Error: class "std::basic_string<char, std::char_traits<char>,std::allocator<char>>" has no member function Compared to.

Any ideas on how to fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: What is wrong with this function and how to fix it

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,680
  • Joined: 25-December 09

Re: What is wrong with this function and how to fix it

Posted 13 May 2012 - 06:25 PM

In your GetItem() function item is a std::string not a RelationType SortedList. How is RelationType defined? In order to use it in a switch it must be an integral type.

Jim

This post has been edited by jimblumberg: 13 May 2012 - 06:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: What is wrong with this function and how to fix it

Posted 13 May 2012 - 06:44 PM

Relation type is defined as
enum RelationType {LESS, GREATER, EQUAL};


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,680
  • Joined: 25-December 09

Re: What is wrong with this function and how to fix it

Posted 13 May 2012 - 06:47 PM

Okay that should work for the switch but item is a std::string not a SortedList.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: What is wrong with this function and how to fix it

Posted 13 May 2012 - 06:55 PM

What do you think I should do?
Was This Post Helpful? 0
  • +
  • -

#6 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: What is wrong with this function and how to fix it

Posted 13 May 2012 - 07:10 PM

I changed the code do you think that this would work, it seemed to fix the error.

	string SortedList::GetItem(string& item, bool& found)
	{
		bool moreToSearch;
		Node* location;
		int x = 0;
		location = listData;
		found = false;
		moreToSearch = (location != NULL);

		while (moreToSearch && !found)
		{
			x = item.compare(location -> info);
			if (x > 0)
			{
				location = location -> next;
				moreToSearch = (location != NULL);
				break;
			}
			if (x == 0)
			{
				found = true;
				item = location -> info;
				break;
			}
			if(x <0)
			{
				moreToSearch = false;
				break;
			}

		}//end while

		return item;
	}



edit: after changing all of the functions that used CompareTo to string.compare it compiles and runs, thank you for pointing that out to me.

This post has been edited by killermunk1: 13 May 2012 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1