![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| 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: * 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 |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#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
}
|
|
|
|
| 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 |
|
|
#2 (permalink) |
|
Registered User
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) |
|
|
|
|
|
#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 |
|
|
|
|
|
#4 (permalink) |
|
Registered User
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) |
|
|
|
|
|
#5 (permalink) |
|
Registered User
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
}
__________________
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) |
|
|
|
![]() |
| Thread Tools | |
|
|