I am getting errors and I feel that is is a small mistake (like a typo or something and I just can't find it. I am hoping that a fresh set of eyes could spot it. I am not done with this project but I would like to clean up some errors before going forward. Any help would be awesome. Here is my code and errors:
#include<iostream>
#include<list>
#include<tchar.h>
usingnamespace std;
int countValue(list<int> front, constint item); //declare the count value function
void writeLinkedList(list<int> front); //declare the write linked list function
int _tmain(int argc, _TCHAR* argv[])
{
list<int> front;
int listCount;
cout << "Enter the size of the list: ";
cin >> listCount;
for (int i = 1; i <= listCount; i++)
front.insert(rand()%5);
cout << "Original List of Values: " << endl;
//writeLinkedList(front, " ");
cout << endl;
for(int j=0;j<5;++j)
cout << countValue (front,j) << endl;
cout << endl;
return 0;
}
int countValue(list<int> front, constint item)
{
int count0;
int count1;
int count2;
int count3;
int count4;
list<int> *List;
for(list<int>::iterator i = front.begin(); i != front.end(); i++)
{
if(List->item == 0)
{
count0++;
}
if(List->item == 1)
{
count1++;
}
if(List->item == 2)
{
count2++;
}
if(List->item == 3)
{
count2++;
}if(List->item == 4)
{
count4++;
}
}
}
And now the errors:
List.cpp
1>c:\users\friedmann\desktop\data structures\assignment 4\list\list\list.cpp(24): error C2661: 'std::list<_Ty>::insert' : no overloaded function takes 1 arguments
1> with
1> [
1> _Ty=int
1> ]
1>c:\users\friedmann\desktop\data structures\assignment 4\list\list\list.cpp(45): error C2039: 'item' : is not a member of 'std::list<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1>c:\users\friedmann\desktop\data structures\assignment 4\list\list\list.cpp(49): error C2039: 'item' : is not a member of 'std::list<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1>c:\users\friedmann\desktop\data structures\assignment 4\list\list\list.cpp(53): error C2039: 'item' : is not a member of 'std::list<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1>c:\users\friedmann\desktop\data structures\assignment 4\list\list\list.cpp(57): error C2039: 'item' : is not a member of 'std::list<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1>c:\users\friedmann\desktop\data structures\assignment 4\list\list\list.cpp(60): error C2039: 'item' : is not a member of 'std::list<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
None of the insert declarations have just one parameter. That's what the compiler is complaining about.
You need to say _where_ you want to insert.
Depending what you're trying to do, solutions for line 19 are:
front.insert(front.begin(), rand()%5);
or
front.push_back(rand()%5);
As regards the function starting at line 30, it has numerous problems. Count variables not initialized, *List not initialized, iterator i is not used, typo in line 55, function never returns a value, so counts are lost, parameter item is not used, all items are counted each time, parameters badly named, etc.
Try:
1 2 3 4 5 6 7 8 9 10
int countValue(const list<int>& int_list, constint value)
{
int count = 0;
for(list<int>::const_iterator it = int_list.begin(); it != int_list.end(); ++it)
{
if(*it == value)
++count;
}
return count;
}