嘗試使用數組列表實現選擇排序。但是,我似乎無法從main調用任何我的列表函數。使用ArrayList實現選擇排序
當執行這段代碼,我收到以下錯誤:
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
我不太清楚如何定義ListItemType功能。
我班的其他人使用的功能和我在他們的主要功能中一樣,但是他們的方法似乎沒有問題。
有點幫助會很好。
頁眉:
/** @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;
};
實現:
/** @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;
}
一般的建議是「做不要在別人使用的頭文件中使用'namespace std;'。 – 2013-03-13 01:06:18
那會是什麼意思? – Methos 2013-03-13 01:17:35
讓我們看看:'[C++] using namespace std' as a search term throws up least [SO 4649003](http://stackoverflow.com/questions/4649003/),[1452721](http://stackoverflow.com/question/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/)。這甚至不努力。 – 2013-03-13 02:18:36