jeudi 27 avril 2017

Explicit invocation of ctor for allocation of memory

Consider the following class:

class Vector{
    int dim; //dimension of array v
    Complex* v; //Complex is another class
 public:
    Vector(int dim = 0):dim(dim){(dim)?(v=new Complex[dim]):(v=nullptr);}
    Vector(int dim, const Complex* c):dim(dim),v(new Complex[dim]){
        for(int i=0;i<dim;i++) v[i]=c[i];}
    Vector(const Vector& a):dim(a.dim),v(new Complex[a.dim]){
        for(int i=0;i<dim;i++) v[i]=a.v[i];}
    ~Vector(){if(dim)delete [] v,v=nullptr;}
    friend Vector& operator >> (Vector& is,Complex& z){
        Vector copie(is);
        is.~Vector();
        is.Vector::Vector(is.dim+1);}
};

I try to overload the >> operator in order to add elements to v. My idea was to create a copy, then call dctor and the ctor for the object to be modified via >> operator. I'm stuck after getting this error:

In function ‘Vector& operator>>(Vector&, Complex&)’:
main.cc:56:20: error: cannot call constructor ‘Vector::Vector’ directly
         is.Vector::Vector(is.dim+1);

I'm not allowed to use containers!! Please help me!

Aucun commentaire:

Enregistrer un commentaire