我开始学习c ++,遇到了这个编译问题。当我更改al2
的大小时,我需要更改al.get(0)
的大小。
完整的编译错误消息:
arraylist.cpp: In instantiation of 'void jpparl::ArrayList<T>::add(T) [with T = jpparl::ArrayList<int>]':
arraylist.cpp:90:15: required from here
arraylist.cpp:46:33: error: use of deleted function 'jpparl::ArrayList<int>& jpparl::ArrayList<int>::operator=(const j
pparl::ArrayList<int>&)'
array[usedSize] = elem;
~~~~~~~~~~~~~~~~^~~~~~
arraylist.cpp:5:30: note: 'jpparl::ArrayList<int>& jpparl::ArrayList<int>::operator=(const jpparl::ArrayList<int>&)' i
s implicitly deleted because the default definition would be ill-formed:
template <class T> class ArrayList {
^~~~~~~~~
arraylist.cpp:5:30: error: non-static reference member 'long int& jpparl::ArrayList<int>::usedSize', can't use default
assignment operator
In file included from arraylist.cpp:2:0:
jppsys.cpp: In instantiation of 'static void jppsys::JPPSystem::arraycopy(T*, long int, long int, T*, long int) [with
T = jpparl::ArrayList<int>]':
jppsys.cpp:10:22: required from 'static void jppsys::JPPSystem::expand(T**, long int, long int) [with T = jpparl::Ar
rayList<int>]'
arraylist.cpp:42:46: required from 'void jpparl::ArrayList<T>::add(T) [with T = jpparl::ArrayList<int>]'
arraylist.cpp:90:15: required from here
jppsys.cpp:5:78: error: use of deleted function 'jpparl::ArrayList<int>& jpparl::ArrayList<int>::operator=(const jppar
l::ArrayList<int>&)'
for (long i = 0; i < srcEnd - srcStart; i++) dest[destStart + i] = src[srcStart + i];
~~~~~~~~~~~~~~~~~~~~^~~~~~
还有我的源代码:
#include <iostream>
#include "jppsys.cpp"
namespace jpparl {
template <class T> class ArrayList {
long l = 0;
long &usedSize = l;
private:
T *array;
int step;
long totalSize;
public:
ArrayList(long step) {
this->step = step;
totalSize = step;
array = new T[step];
}
ArrayList() {
this->step = 8;
totalSize = step;
array = new T[step];
}
void add(T elem, long index) {
if (usedSize == totalSize) totalSize += step;
T *tmp = new T[totalSize];
jppsys::JPPSystem::arraycopy(array, 0, index, tmp, 0);
jppsys::JPPSystem::arraycopy(array, index, usedSize, tmp, index + 1);
delete[] array;
tmp[index] = elem;
array = tmp;
usedSize++;
}
void add(T elem) {
if (usedSize == totalSize) {
jppsys::JPPSystem::expand(&array, usedSize, usedSize + step);
totalSize += step;
}
std::cout << usedSize << " add\n";
array[usedSize] = elem;
usedSize++;
}
void remove(long index) {
if (usedSize == totalSize - step) totalSize -= step;
T *tmp = new T[totalSize];
jppsys::JPPSystem::arraycopy(array, 0, index, tmp, 0);
jppsys::JPPSystem::arraycopy(array, index + 1, usedSize, tmp, index);
delete[] array;
*array = *tmp;
usedSize--;
}
void remove() {
if (usedSize == totalSize - step) {
jppsys::JPPSystem::expand(&array, usedSize - step, usedSize - step);
totalSize -= step;
}
usedSize--;
}
T get(long index) {
return array[index];
}
long size() {
return usedSize;
}
long getTotalSize() {
return totalSize;
}
};
}
using namespace jpparl;
using namespace std;
int main() {
ArrayList<ArrayList<int>> al;
ArrayList<int> al2;
al.add(al2);
al2.add(256);
cout << al2.size() << " " << al.get(0).size() << "\n";
}
感谢您的帮助
答案 0 :(得分:0)
template <class T>
class ArrayList {
long l = 0;
long &usedSize = l;
在您的类中具有引用会隐式删除您的operator=
,您需要重写它,而且我也不确定为什么要对类变量进行私有引用吗?如果删除引用并仅使用l
变量,它将起作用。
还有#include "jppsys.cpp"
不包括.cpp
个文件。