Linked List

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:

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
#include<iostream>
#include<list>
#include<tchar.h>

using namespace std;

int countValue(list<int> front, const int 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, const int 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> ]

Thanks again!
Line 19, insert doesn't take one argument, use push_back. There also is no item member of std::list, lines 45, 49, 53, 57, and 60.

http://www.cplusplus.com/reference/list/list/
Last edited on
Check out the top of:
http://www.cplusplus.com/reference/list/list/insert/

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, const int value)
{
  int count = 0;
  for(list<int>::const_iterator it = int_list.begin(); it != int_list.end(); ++it)
  {
    if(*it == value)
      ++count;
  }
  return count;
}
Topic archived. No new replies allowed.