Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 




Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.


Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]
Reply
 
LinkBack Thread Tools
Old 12-04-2005, 03:22 PM   #1 (permalink)
Registered User
 
Join Date: Apr 2005
Posts: 25
OS: server 2003


C++ Help!

have a homework assignment I am having trouble with. The instructions are:

Implement the function maxLoc(), which return an iterator pointing at the largest element in a list.

// return an iterator pointing to the largest element
// in the list.
templete <typename T>
list<T>::iterator maxLoc(list<T>& aList);
Write a program that tests maxLoc(), using the following declarations:
string strArr[] = {"insert", "erase", "templete", "list"};
int strSize = sizeof(strArr) / sizeof(string);
list<string> strList(strArr, strArr+strSize);
The program should repeatedly call maxLoc(),
output the largest value, and then delete the value, until the list is empty.

When I try to debug the program, I keep getting an error. If I say no, it breaks. If I say yes to continue it executes fine. The error is:

error C2664: 'maxLoc' : cannot convert parameter 1 from 'std::list<_Ty>::iterator' to 'std::list<_Ty> &'
with
[
_Ty=std::string
]
and
[
_Ty=std::string
]

Anyone have an idea what I am doing wrong? Here is my code:

Code:
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>

using namespace std;

list<string>::iterator maxLoc(list<string>& aList);

int main()
{		

		string strArr[] = {"insert","erase","template","list"};
		int strSize = sizeof(strArr)/sizeof(string);
		list<string> aList(strArr, strArr + strSize);
		list<string>::iterator iter = aList.begin();
		list<string>::iterator max = aList.begin();


		//validate that the list is not empty.  Run loop until aList.empty() = true
		while (!aList.empty())
		{
			cout << "The list contains the elements " << endl;

			//call the maxLoc function and pass max into main()
			maxLoc(max);

			//Output the max element that was found
			cout << "The Max Element is " << *max << endl << endl;

			//erase the element found at maxLoc from teh list
			cout << "Press Enter to remove " << *max << " from the list and continue." << endl << endl;
			aList.erase(max);
			

			//reset the iter and maxLoc pointers
			iter = aList.begin();
			max = aList.begin();
			
			cin.get();
		}//end loop

	cout << "The List is Empty." << endl;
	cout << "Press Enter to Continue";
	cin.get();
	return 0;
	
}//end main()	



list<string>::iterator maxLoc(list<string>& aList)
{			
	list<string>::iterator iter = aList.begin();
	list<string>::iterator max  = aList.begin();
	
	
	//loop reads through the list and sets the maxLoc to equal iter if iter is greater than the current maxLoc
		while (iter!=aList.end())
		{			
			cout << *iter << endl;
			if (*iter > *max)
				max = iter;
			++iter;	
			return max;
		}//end loop			
}
arstacey is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 12-05-2005, 12:08 PM   #2 (permalink)
Registered User
 
ricer333's Avatar
 
Join Date: Sep 2004
Posts: 306
OS: Vista (on Laptop) WinXP Pro SP3 (on Desktop)


You need to pass it a pointer to max, not the iterator itself. Your maxLoc function is expecting and address (signified by '&'), so when you call maxLoc in the main(), send it a pointer to max (*max).

So something like: maxLoc(*max)

If I read it correctly, that should fix it.
__________________
AMD X2 5000+ Black w/ XFX GeForce 8200 MoBo
4GB OCZ DDR2 RAM
3/4 of a TB of data storage between 2 HDD's (no RAID)
ricer333 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-01-2007, 06:35 PM   #3 (permalink)
Registered User
 
Join Date: May 2007
Location: Nashville, TN
Posts: 1
OS: NT


Re: C++ Help!

Good evening Ricer333 and thank you also for your assistance.

I also have this assignment.
Originally, I had coded it differently.

I too am receiving the same error message as arstacey.

I tried your code, but I still get the same error.

I changed my code to match arstacy's and still am getting that dog gone error.

I appreciate your help in this matter, your suggestion made allot of sense.

Is there anything else you see that could be causing this error?

I took the bit of code out but now the program maxLoc does not locate the highest element in the array, it merely outputs sequence of the array as is shown in the array.

Thank you and have a great night.

Nash
nash is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-30-2007, 03:04 PM   #4 (permalink)
Registered User
 
ricer333's Avatar
 
Join Date: Sep 2004
Posts: 306
OS: Vista (on Laptop) WinXP Pro SP3 (on Desktop)


Re: C++ Help!

Sorry I haven't replied back till now. The problem lies within the types that are being passed in. The function maxLoc is assuming a list type of string and not a list type of string::iterator. Max (being passed in, not the max being declared locally) is of type list<string>::iterator.
__________________
AMD X2 5000+ Black w/ XFX GeForce 8200 MoBo
4GB OCZ DDR2 RAM
3/4 of a TB of data storage between 2 HDD's (no RAID)
ricer333 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-30-2007, 03:12 PM   #5 (permalink)
Registered User
 
ricer333's Avatar
 
Join Date: Sep 2004
Posts: 306
OS: Vista (on Laptop) WinXP Pro SP3 (on Desktop)


Re: C++ Help!

Now, this compiles, but really without knowing what is supposed to go on I can't say it's right. But compare with what you both had and with what I did. You'll see now that I am passing in the right type of parameter to the maxLoc function. I'm also catching the return value (retVal) from that function call.

Code:
#include <iostream>
#include <list>
#include <string>
#include <cstdlib>

using namespace std;

list<string>::iterator maxLoc(list<string>& aList);

int main()
{		

		string strArr[] = {"insert","erase","template","list"};
		int strSize = sizeof(strArr)/sizeof(string);
		list<string> aList(strArr, strArr + strSize);
		//RICER333 list<string>::iterator iter = aList.begin();
		//RICER333 list<string>::iterator max = aList.begin();
		list<string>::iterator retVal;


		//validate that the list is not empty.  Run loop until aList.empty() = true
		while (!aList.empty())
		{
			cout << "The list contains the elements " << endl;

			//call the maxLoc function and pass max into main()
			//RICER333 maxLoc(max);
			retVal = maxLoc(aList);

			//Output the max element that was found
			cout << "The Max Element is " << *retVal << endl;

			//erase the element found at maxLoc from teh list
			// RICER333 cout << "Press Enter to remove " << *max << " from the list and continue." << endl << endl;
			// RICER333 aList.erase(retVal);
			

			//reset the iter and maxLoc pointers
			// RICER333 iter = aList.begin();
			// RICER333 max = aList.begin();
			retVal = aList.begin();
			
			cin.get();
		}//end loop

	cout << "The List is Empty." << endl;
	cout << "Press Enter to Continue";
	cin.get();
	return 0;
	
}//end main()	



list<string>::iterator maxLoc(list<string>& aList)
{			
	list<string>::iterator iter = aList.begin();
	list<string>::iterator max  = aList.begin();
	
	
	//loop reads through the list and sets the maxLoc to equal iter if iter is greater than the current maxLoc
		while (iter!=aList.end())
		{			
			cout << *iter << endl;
			if (*iter > *max)
				max = iter;
			++iter;	
			return max;
		}//end loop			
}
Again, sorry I haven't been on in awhile. Hopefully I have showed you the error that you were having.
__________________
AMD X2 5000+ Black w/ XFX GeForce 8200 MoBo
4GB OCZ DDR2 RAM
3/4 of a TB of data storage between 2 HDD's (no RAID)
ricer333 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 09:27 AM.



Copyright 2001 - 2010, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84