2013-03-13 73 views
0

嘗試使用數組列表實現選擇排序。但是,我似乎無法從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; 
} 
+1

一般的建議是「做不要在別人使用的頭文件中使用'namespace std;'。 – 2013-03-13 01:06:18

+0

那會是什麼意思? – Methos 2013-03-13 01:17:35

+0

讓我們看看:'[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

回答

1

存在幾個誤區:

int listofitems[numberofitems]; 

numberofitems需要是const,自ListItemType我猜是stringlistofitems應該b e是一組string s。這是delcaring功能:

List myArrayList(); 

你打算什麼是實例化一個List,所以它應該是:

List myArrayList ; 

此:

int mainarraylistsize = (myArrayList.getLength); 

應該是:

int mainarraylistsize = myArrayList.getLength() ; 

這個參數列表是不正確的:

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

論證兩者應該是一個ListItemType類型,但你傳遞一個int,這大概是你想要的結果:

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

我對一個ListItemType究竟是什麼感到困惑。我的意思是沒有完全解釋。至於我擔心它是一個參考變量,但我不知道如何定義它。 – Methos 2013-03-13 01:10:17

+0

@Methos所以你有'typedef string ListItemType;'在頭的頂部,這意味着'ListItemType'是一個'string',你可以在這裏閱讀'typedef' http://en.wikipedia.org/wiki/ Typedef – 2013-03-13 01:12:57

+0

當我在檢索函數中將ListItemType定義爲字符串時,它給我一個錯誤。我不認爲ListItemType應該是一個字符串。 – Methos 2013-03-13 01:15:55