JavaのArrayListに似たC++のテンプレートクラスに取り組んでいます(はい、ベクトルが同じことを行うことは知っています。これは実用的なコーディングプロジェクトではありません)。
別の ArrayList を引数として取り、ArrayList をシードする ArrayList クラスのコンストラクターがあれば便利だと思いました。しかし、コンストラクターを書き込もうとすると、このエラーが発生します
invalid constructor; you probably meant 'ArrayList<T> (const ArrayList<T>&)'
これは、ArrayList が定数でなければならないということですか? また、なぜ addressof 演算子が必要なのですか?
私はまだ C++ のロープを学んでいるので、少し混乱しています。
プロトタイプはここにあります:
ArrayList(ArrayList<T> list);
ArrayList(ArrayList<T> list, int size);
コードは次のとおりです。
/**
* Creates an ArrayList of type T that is twice the
* size of the passed in ArrayList, and adds all elements
* from the passed ArrayList<T> list, to this ArrayList.
*
* Runs in O(n) time, where n = the size of list.
*
* @param list the ArrayList to use as a seed for this ArrayList.
*/
template<class T>
ArrayList<T>::ArrayList(ArrayList<T> list) {
array = new T[list.getSize() * 2];
capacity = list->getSize() * 2;
size = list->getSize();
for (int i = 0; i < list->getSize(); i++) {
array[i] = list->get(i);
}
}
編集 以下のコードではエラーが発生しませんが、上記のコードではエラーが発生します.....
/**
* Creates an ArrayList of type T that has a capacity equal to the passed
* in theCapacity parameter. This ArrayList starts with the passed ArrayList.
*
* Note: If the passed in capacity is smaller than the size of the passed in
* ArrayList, then the capacity is set to twice the size of the
* passed ArrayList.
*
* @param list the ArrayList to use as a seed for this ArrayList.
* @param theCapacity the capacity for this ArrayList.
*/
template<class T>
ArrayList<T>::ArrayList(ArrayList<T> list, int theCapacity) {
if (theCapacity >= list->getSize()) {
array = new T[theCapacity];
capacity = theCapacity;
}
else {
array = new T[list->getSize() * 2];
capacity = list->getSize() * 2;
}
size = list->size;
for (int i = 0; i < size; i++) {
array[i] = list->get(i);
}
}