# C++ Help!



## arstacey (Apr 14, 2005)

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:


```
#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			
}
```


----------



## ricer333 (Sep 17, 2004)

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.


----------



## nash (May 2, 2007)

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


----------



## ricer333 (Sep 17, 2004)

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.


----------



## ricer333 (Sep 17, 2004)

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.


```
#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.


----------

