[petsc-users] Assignement operator overloading for vectors

Cedric Doucet cedric.doucet at inria.fr
Thu Jun 19 07:30:56 CDT 2014


I have the same error in copy contructor :

Foo::Foo(Foo const & copy)
{
  VecDuplicate(copy.m_vec,&m_vec);
  VecCopy(copy.m_vec,m_vec);
}

What am I doing wrong here?

Cédric



----- Mail original -----
> De: "Cedric Doucet" <cedric.doucet at inria.fr>
> À: petsc-users at mcs.anl.gov
> Envoyé: Jeudi 19 Juin 2014 14:14:39
> Objet: Assignement operator overloading for vectors
> 
> Hello,
> 
> I need to overload the assignement operator for a C++ class which has a Petsc
> vector as a data member:
> 
> class Foo
> {
> public:
>   Foo & operator=(Foo const & copy);
> private:
>   Vec m_vec;
> };
> 
> The algorithm for overloading the assignement operator should look like this
> :
> 
> Foo & Foo::operator=(Foo const & copy)
> {
>   if ( this != copy )
>   {
>     // destroy this->m_vec
>     // allocate this->m_vec with the same size as copy.m_vec's size
>     // copy the content of copy.m_vec into this->m_vec
>   }
>   return *this;
> }
> 
> I thought that VecCopy(copy.m_vec,m_vec) does everything I need but I have an
> error during the execution.
> So I tried to call first VecDuplicate(copy.m_vec,&m_vec) and then
> VecCopy(copy.m_vec,m_vec) but I still have an error.
> How shoud assignement operator overloading be implemented?
> Do I have to call VecDestroy before calling VecDuplicate?
> Must input vectors have the same size in VecCopy?
> 
> Thank you very much for your help!
> 
> Best regards,
> 
> Cédric Doucet


More information about the petsc-users mailing list