这是 arrayList 类:
template<class T>
class arrayList: public linearList<T>
{
public:
// constructor, copy constructor and destructor
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T>&);
~arrayList() {
delete[] element;
}
void insert(int theIndex, const T& theElement);
protected:
T* position;
}; // end of iterator class
protected:
// additional members of arrayList
void checkIndex(int theIndex) const;
// throw illegalIndex if theIndex invalid
T* element; // 1D array to hold list elements
int arrayLength; // capacity of the 1D array
int listSize; // number of elements in list
};
dict2 读取文件并将单词存储在 arrayList 中。我用 rrayList<char[10]>
,但是如何从文件中将这些输入到 arrayList 中呢?上面的 main() 函数中指出了错误。
主要是:
在main函数中,有如下内容。
arrayList<char[10]> *dict1 = new arrayList<char[10]> (1000);
int k = 0;
while (getline(fin, str)) {
dict1->insert(k, str.c_str()); // error here
k++;
}
reverseArray(dict2); // error here
编辑
我应该使用 arrayList<string>
在这种情况下。
最佳答案
您的insert
函数声明为
void insert(int theIndex, const T& theElement);
T
是 char[10]
,因此您需要将 char[10]
传递给它,而不是 const字符*
。
数组既不可赋值也不可复制构造,因此如果您希望容器能够处理它们,则需要专门针对值类型为数组的情况编写代码:它们通常不能在与处理标量对象的方式相同。
关于c++ - arrayList<string> 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4867912/