CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    Copy constructor for std::vector ???

    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:
    Code:
    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?
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Copy constructor for std::vector ???

    The copy constructor takes a const reference only. When initializing members upon construction use initializer lists whenever that´s possible.

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

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Copy constructor for std::vector ???

    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:

    Code:
    this->Conditions = ctrl.Conditions;
    3) What are the types of the variables ? You might not even need
    a copy constructor.

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Copy constructor for std::vector ???

    Did you perhaps make the CONTROL copy constructor explicit?
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Copy constructor for std::vector ???

    Quote Originally Posted by nemok
    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.
    Code:
    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

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Copy constructor for std::vector ???

    Quote Originally Posted by GNiewerth
    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

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Copy constructor for std::vector ???

    Copy constructors can have any of the following 4 forms below:
    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured