I started learning c ++ and I ran into this compilation problem. When I change size of al2 I need to change size in al.get(0).
Full compilation error message:
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]; ~~~~~~~~~~~~~~~~~~~~^~~~~~ And my source code:
#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"; } I will be grateful for help
usedSizea reference? (It's even a reference tol, which you never use.)lis not a good variable name, it took me some time to understand what he meant, but now I am completely on his side ;)