gpt4 book ai didi

c++ - 使用 ArrayList 实现选择排序

转载 作者:行者123 更新时间:2023-11-28 07:43:25 25 4
gpt4 key购买 nike

尝试使用数组列表实现选择排序。但是,我似乎无法从 main 调用我的任何列表函数。

执行此代码时,我收到以下错误:

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 函数。

我类的其他人使用的函数与我在他们的 main 中使用的函数完全相同,但他们的方法似乎没有问题。

一点帮助就好了。

标题:

/** @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;
}

最佳答案

有几个错误:

int listofitems[numberofitems];

numberofitems 需要是 const我猜因为 ListItemTypestring,所以 listofitems 应该是 string 的数组。这是一个函数:

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);

关于c++ - 使用 ArrayList 实现选择排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15375020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com