0

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:

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:

/** @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:

    /** @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;
 }
Methos
  • 9
  • 1
  • 1
  • 7
  • 1
    The general advice is 'do not use `using namespace std;` in headers that are for other people to use'. – Jonathan Leffler Mar 13 '13 at 01:06
  • What would the point of that be? – Methos Mar 13 '13 at 01:17
  • Let's see: `[c++] using namespace std` as a search term throws up at least [SO 4649003](http://stackoverflow.com/questions/4649003/), [1452721](http://stackoverflow.com/questions/1452721/), [SO 7134403](http://stackoverflow.com/questions/7134403/), [SO 14575799](http://stackoverflow.com/questions/14575799/), [SO 5469060](http://stackoverflow.com/questions/5469060/), [SO 5849457](http://stackoverflow.com/questions/5849457/). And that's not even trying very hard. – Jonathan Leffler Mar 13 '13 at 02:18

1 Answers1

1

There are several errors:

int listofitems[numberofitems];

numberofitems needs to be a const and I am guessing since ListItemType is string that listofitems should be an array of strings. This is delcaring a function:

List myArrayList();

what you intend is to instantiate a List and so it should be:

List myArrayList ;

This:

int mainarraylistsize = (myArrayList.getLength);

should be:

 int mainarraylistsize = myArrayList.getLength() ;

and this argument list is not correct:

 myArrayList.insert(listofitems[i], 1, mainsucc);

Argument two should be a ListItemType type but you are passing in an int, this it probably what you intended:

 myArrayList.insert(i, listofitems[i], mainsucc);
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • I'm a bit confused about what exactly a ListItemType is. I mean it isn't exactly explained. As far as I'm concerned it's a reference variable but I'm not sure how to define it at all. – Methos Mar 13 '13 at 01:10
  • @Methos So you have `typedef string ListItemType;` at the top of the header which means that `ListItemType` is a `string`,you can read about `typedef` here http://en.wikipedia.org/wiki/Typedef – Shafik Yaghmour Mar 13 '13 at 01:12
  • When I define the ListItemType as a string in the retrieve function it gives me an error. I don't think ListItemType is supposed to be a string at all. – Methos Mar 13 '13 at 01:15