1

I have a vector of user defined objects such as

std::vector<UserDefinedClass> list_of_objects;

The UserDefinedClass doesnt have any explicit copy constructor.

Now I would like to make a copy of them. So, I define

std::vector<UserDefinedClass> list_of_objects_copied;
list_of_objects_copied = list_of_objects;

Do I really need a copy constructor to iterate the vector and copy the objects in the vector one by one?

The error is

error: use of deleted function ‘UserDefinedClass& UserDefinedClass::operator=(const UserDefinedClass&)’

If I use a built in object such as int, double etc, I dont have any problem in copying the list.

Class definition

UserDefinedClass {
private:
    int &m_a;
public:
    UserDefinedClass(int a):m_a(a) {};
};
infoclogged
  • 3,641
  • 5
  • 32
  • 53
  • 1
    I think you're using the term "copy constructor" incorrectly. Your options are assignment of vectors or a for-loop. The second option has nothing to do with copy constructor. If you disagree, please clarify your question. – anatolyg Feb 27 '18 at 16:39
  • No you don't if you dont explicitly define any other except the constructor, which you probably did. – Hatted Rooster Feb 27 '18 at 16:39
  • https://stackoverflow.com/questions/644673/fast-way-to-copy-one-vector-into-another – nullqube Feb 27 '18 at 16:39
  • 1
    Show your class. – Hatted Rooster Feb 27 '18 at 16:39
  • @anatolyg you are right, its not the copy constructor of the UserDefinedClass, but the copy constructor of the class Vector. Can the class Vector only handle inbuilt objects such as int, double etc.? – infoclogged Feb 27 '18 at 16:44
  • @nullqube . the link with the answer https://stackoverflow.com/a/644677/1534898 is exactly my problem. I dont have a built in object such as int but a user defined object. – infoclogged Feb 27 '18 at 16:48
  • Reference members is one of those ideas that seem really good until you try to do it. – molbdnilo Feb 27 '18 at 17:10

3 Answers3

4

Your UserDefinedClass has a reference member variable. Reference members can't be rebound after construction, so the class is not copy-assignable by default, which means a vector of them also can't be copy-assigned.

You can copy-construct to create a new copy of the items, or depending on your needs you could create your own copy-assignment operator that does something different with the reference member.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • is that the reason that the gcc compiler produces this error, because the copy constructor of classes with reference variables are automatically deleted? – infoclogged Feb 27 '18 at 18:23
0

Yes if you want to copy your objects by copying them into another vector you need a copy constructor. If you want to avoid the copy you can either move the first vector into the second which would invalidate the first vector or store pointers in your vector (ideally a shared_ptr) which can then be copied without copying the underlying objects.

To make your class copyable change the reference member to a pointer:

UserDefinedClass {
private:
    int *m_a;
public:
    UserDefinedClass(int& a):m_a(&a) {};
};

You can still keep the parameter passed to the constructor a reference to guarantee that the value is not null.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
0

A reference has to be initialized. When you do this you are trying to assign to the reference after it's been created.

What would work is to construct a copy from the other vector.

std::vector<UserDefinedClass> list_of_objects = { ... };
std::vector<UserDefinedClass> list_of_objects_copied{list_of_objects};
super
  • 12,335
  • 2
  • 19
  • 29