To put it simply, my compiler refuses to call the copy constructor and the assignment operator which I provided.
I had a project for university to make a database-like program in C++ using architecture stratification(domain, repository, service and UI). Initially I was asked to use std::vector
and I completed the program without any struggle. But now I have to replace std::vector
with... a simply linked list
I've pretty much finished writing the linked list and I've tested all of its functionalities. However, when I implement it in the program, replacing the std::vector
, the compiler won't recognize the copy constructor and the assignment operator!
Here is part of the Linked List implementation(without the Node
class and ForwardIterator
class which I've tested and don't seem to cause any issues)
template <class ElemType>
class LinkedList
{
Node<ElemType> *front, *back;
public:
LinkedList() : front{ NULL }, back{ NULL } {}
//The problematic copy constructor
LinkedList(LinkedList<ElemType>& lst) : LinkedList() {
for (auto& elem : lst)
push_back(elem);
}
~LinkedList() {
while (front != NULL)
erase(begin());
}
//The problematic assignment operator
LinkedList<ElemType>& operator=(LinkedList<ElemType>& lst) {
while (front != NULL)
erase(begin());
for (auto& elem : lst)
push_back(elem);
return *this;
}
I get the following errors multiple times:
E0334 class "LinkedList< Medicament >" has no suitable copy constructor
E0349 no operator "=" matches these operands
C2440 'initializing': cannot convert from 'LinkedList< Medicament >' to 'LinkedList< Medicament >'
C2679 binary '=': no operator found which takes a right-hand operand of type 'LinkedList< Medicament >' (or there is no acceptable conversion)
I would normally make the parameters and auto's const
, but that gives me another error(no idea why), despite push_back()
taking a const reference:
C2662 'ForwardIterator< Medicament > LinkedList< Medicament >::begin(void)': cannot convert 'this' pointer from 'const LinkedList< Medicament >' to 'LinkedList< Medicament > &'
The main errors(copy constructor and assignment) only happen when I pass the argument as a return value of a member function from within the service/repository/ui . A short example that causes this issue:
class MediRepo { //repository
public:
LinkedList<Medicament> elem; //I made it public for now
//............
//Returns a copy of the medicine list
LinkedList<Medicament> getMeds() { return elem; }
}
//main
MediRepo rep;
LinkedList<Medicament> medlist{ rep.getMeds() }; //ERROR E0334 and C2440
LinkedList<Medicament> medlist{ rep.elem }; //OK
Like I said, the linked lists(including the constructor and the assignment operator) work perfectly fine outside of the service/repository. If I spend more time, I can probably come up with a half-assed workaround to this, but I hope somebody can tell me what I am doing wrong that gives me these errors(the one with const parameters, too, if possible). I've spent way too many hours trying to solve this... Also, please ask if you would like to see more of the source code.