0

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

4
  • 1
    The error message clearly explains that since your ArrayList class has usedSize member that is reference to long it can not be assigned with = because the references can not be reseated. Commented Aug 10, 2018 at 13:40
  • A class with a reference member gets a deleted assignment operator. In approximately 100% of the cases, a reference member is not something that you need. Why is usedSize a reference? (It's even a reference to l, which you never use.) Commented Aug 10, 2018 at 13:40
  • @molbdnilo objects with reference members can certainly be copy constructed but what OP code is doing is copy assignment. Commented Aug 10, 2018 at 13:42
  • btw in school i made fun of a teacher because he tried to convince me that l is not a good variable name, it took me some time to understand what he meant, but now I am completely on his side ;) Commented Aug 10, 2018 at 13:44

1 Answer 1

1
template <class T> class ArrayList { long l = 0; long &usedSize = l; 

Having a reference in your class implicitly deletes your operator=, you need to rewrite it, also I'm not really sure why you would want a private reference to your class variable? If you remove the reference and just use the l variable it should work.

Also, #include "jppsys.cpp"

don't include .cpp files.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, how do I make the size of al.get(0) change when changing the size of al2? If I do not use reference, the usedSize will not change in al.get(0), only in al2
@congard why would you want the size of one list to be changed when another list is modified? Regardless, you can use a pointer instead of a reference. Pointers are assignable.
al.get(0) and al2 are different variables, when you use al.add(al2), you are making a copy of al2, so al.get(0) and al2 are 2 different objects now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.