I have the class OpenTable
in my code:
class OpenTable : public BaseAction {
public:
OpenTable(int id, std::vector<Customer *> &customersList);
OpenTable(const OpenTable& other);
private:
const int tableId;
const std::vector<Customer *> customers;
};
I need to implement a copy constructor for this class (I know it's probably a bad design, but I was instructed to do so). I ran into a problem when trying to deep copy the const vector customers
.
I tried the following:
OpenTable::OpenTable(const OpenTable& other): tableId(other.tableId)
{
for(std::vector<Customer*>::const_iterator i=other.customers.begin(); i!=other.customers.end(); i++) {
customers.push_back((*i)->copy()); // copy returns a Customer*
}
But clearly it doesn't compile, probably because the vector is const
, thus I can't add elements to it.
I get the following error:
no instance of overloaded function "std::vector<_Tp, _Alloc>::push_back
[with _Tp=Customer *, _Alloc=std::allocator<Customer *>]" matches the
argument list and object (the object has type qualifiers that prevent a
match) -- argument types are: (Customer *) -- object type is: const
std::vector<Customer *, std::allocator<Customer *>>
Note: In the parameterized constructor I simply shallow copied because I could. This won't work for the copy constructor though.
Thanks in advance.