Implementing an Array Based List class to perform a selection sort

Trying to implement a selection sort using an array list. However, I can't seem to be able to call any of my list functions from main.

When this code is executed, I recieve the following errors:

1
2
3
arraylist.cpp: In function ‘int main()’:
arraylist.cpp:92:49: error: no matching function for call to ‘List::retrieve(int, const char [4], bool&)’
arraylist.cpp:47:6: note: candidate is: void List::retrieve(int, ListItemType&, bool&) const


I am not quite sure how to define the ListItemType function.

Other people in my class have used the same exact functions as I have in their main but their methods seem to work without a problem.

A little help would be nice.


Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** @file ListA.h */
#include <string>
using namespace std;
const int MAX_LIST = 10;
typedef string ListItemType;
class List
{
	
public:
   List();
   bool isEmpty() const;
   int getLength() const;
   void insert(int index, const ListItemType& newItem, bool& success);
   void retrieve(int index, ListItemType& dataItem, bool & success) const;
   void remove(int index, bool& success);
private:
	ListItemType items[10];
	int size;
	int translate(int index) const;
};


Implementation:

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
85
86
87
88
89
90
91
92
93
94
95
96
/** @file ListA.cpp */

#include "ArrayList.h"  // header file
#include <iostream>
#include <fstream>

List::List() : size(0)
{
}
bool List::isEmpty() const
{
   return size == 0;
}
int List::getLength() const
{
   return size;
}
void List::insert(int index, const ListItemType& newItem,
bool& success)
{
success = (index >= 1) &&
(index <= size + 1) &&
(size < MAX_LIST);
   if (success)
   {
      for (int pos = size; pos >= index; --pos)
         items[translate(pos + 1)] = items[translate(pos)];
      items[translate(index)] = newItem;
      ++size;  // increase the size of the list by one
   }  
}  

void List::remove(int index, bool& success)
{
   success = (index >= 1) && (index <= size);
   if (success)
   {
      for (int fromPosition = index + 1;
	   fromPosition <= size;
	   ++fromPosition)
         items[translate(fromPosition - 1)] = items[translate(fromPosition)];
      --size;  // decrease the size of the list by one
   }  // end if

}  // end remove

void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
   success = (index >= 1) && (index <= size);
   if (success)
      dataItem = items[translate(index)];
}

int List::translate(int index) const
{
   return index - 1;
}
 int main()
 {
 int var1 = 1;
 int numberofitems;
 int n = 0;
 int p = 0;
 cout << "Please enter the number of data items:" << endl;
 cin >> numberofitems;
 cout << endl;
 cout << "Please enter the data items, one per line:" << endl;
 int listofitems[10];
 //string mainlistitemptype = "int";
 List myArrayList;
 cout << myArrayList.getLength() << endl;
		if (myArrayList.isEmpty())  // tests before
	{
		cout << "This list is empty \n" << endl;
	}
	else
	{
		cout << "List is not empty! \n"<< endl;
	}
 //myArrayList.size(numberofitems);
 bool mainsucc = false;
 int mainarraylistsize = myArrayList.getLength();
 for (int i = 0; i<numberofitems; i++)
 {
 cout << "Enter number " << i + 1 << " : " ;
 cin >> listofitems[i];
 myArrayList.insert(listofitems[i], "int", mainsucc);
 }
 for (int i=0; i<mainarraylistsize; i++)
 {
 cout << myArrayList.retrieve(0, "int", mainsucc);
 }
 return 1;
 }
Last edited on
Ok. First of all, you dont require Node and selectionSortLinked and swap.
Since you are supposed to implement list using arrays (according to the .h file).
The implementations of insert, remove, etc look ok.

In main(), you are calling them wrong.
For eg, insert takes 2nd parameter as ListItemType (which is typedef as string).
However you are sending an array of integers.
Similarly, give correct values for all the functions you are calling from main().

One more thing. The 3rd parameter is bool& success. It is a reference.
May be you want to read up reference variables and their usage in function parameters.
Usually, we pass something as reference so that the value can reflect at the calling location as well. Means, if the value of success is modified inside the function, that will affect the value of the variable at the calling location as well. In this context, they are using this reference variable instead of return values. So you might want to send a variable in the calling function and then check its value to make sure that the function call is successful. Something like..
1
2
3
4
bool succ = false;
myArrayList.remove(1,succ);
if(succ==false)
    cout<<"Remove was not successful"<<endl; 
How do I define a ListItemType then?
Define it like you define any other variable...
May be like this.....
ListItemType item = "listItem1";

Since the typedef for ListItemType is already included in the header file that you are including, the definition would remain intact here.
You could also define it as a string directly:
string item = "listItem1";
Topic archived. No new replies allowed.