Click to See Complete Forum and Search --> : Copy constructor for std::vector ???


nemok
April 11th, 2008, 07:09 AM
Hello,

Does the vector class have a copy constructor. If yes why isn't it working for me.
I have this copy constructor for my own class:

CONTROL(CONTROL& ctrl)
{
this->bNot = ctrl.bNot;
this->type = ctrl.type;
this->value = ctrl.value;
this->Conditions = ctrl.Conditions;
}

where Conditions is a std::vector and I am trying to copy it.
When i compile I get the error: Error 8 error C2558: class 'CONTROL' : no copy constructor available or copy constructor is declared 'explicit' c:\program files\microsoft visual studio 8\vc\include\vector 1125


What have i done wrong? Should I create the new vector and copy element by element?

GNiewerth
April 11th, 2008, 07:14 AM
The copy constructor takes a const reference only. When initializing members upon construction use initializer lists whenever thatīs possible.


CONTROL( const Control& other ) :
bNot( other.bNot ),
type( other.type ),
value( other.value ),
conditions( other.conditions )
{
}

Philip Nicoletti
April 11th, 2008, 08:19 AM
1) First, I agree ... the parameter should be a const reference.

2) I assume the problem is that you are creating a vector<CONTROL>,
and getting the error when trying to do a push_back, as the following
line does not require ctrl to be const:


this->Conditions = ctrl.Conditions;


3) What are the types of the variables ? You might not even need
a copy constructor.

Zaccheus
April 11th, 2008, 08:23 AM
Did you perhaps make the CONTROL copy constructor explicit?

Paul McKenzie
April 11th, 2008, 08:28 AM
Hello,

Does the vector class have a copy constructor.Yes it does have a copy constructor. If yes why isn't it working for me.You need to post your class, not a function of the class. Vector requires that class is copy constructible.

int main()
{
CONTROL c1;
CONTROL c2 = c1; // copy constructor
CONTROL c3;
c3 = c1; //assignment
}

This code doesn't use vector, but if you have the problems you stated, this code shouldn't work either (either it won't compile, or if it does, doesn't work correctly at run time).

And as others have pointed out, you may not even need to write a user-defined copy constructor. We will only know this if we see what member variables your class has.

Regards,

Paul McKenzie

Paul McKenzie
April 11th, 2008, 08:37 AM
The copy constructor takes a const reference only.Not really.

A copy constructor can take a const or non-const reference and can have no other arguments, or any other arguments must have default values.

Regards,

Paul McKenzie

exterminator
April 11th, 2008, 11:52 PM
Copy constructors can have any of the following 4 forms below:

A(A& rhs /*, either no other args or all default args*/);
A(const A& rhs /*, either no other args or all default args*/);
A(volatile A& rhs /*, either no other args or all default args*/);
A(const volatile A& rhs /*, either no other args or all default args*/);
The shown part of the code does not need to be the culprit. Showing the relevant class will be needed in order to find out where from the error comes.

But if the copy constructor has the first form (from above), it will disallow copy from const objects or temporaries. So, that could be one possible cause.