Вопрос

i have the next errors when i try to compile my code:

./usuario.h:64:37: error: call to implicitly-deleted copy constructor of 'Cadena'
                Cadena direccion() const { return direccion_; }
                                                  ^~~~~~~~~~
./../P1/Cadena/cadena.h:11:3: note: copy constructor is implicitly deleted because 'Cadena' has a user-declared move constructor
                Cadena(Cadena&& c);
usuario.cpp:14:3: error: call to implicitly-deleted copy constructor of 'Cadena'
         nombre_(nombre), apellidos_(apellidos), direccion_(direccion), password_(password){
         ^       ~~~~~~

The constructors in Cadena are:

Cadena(const size_t t=0, const char c=' ');
Cadena(Cadena&& c);
Cadena(const char* c);

And the move-constructor:

Cadena::Cadena(Cadena&& c) : tam_(c.tam_) {
    cadena_ = new char[tam_+1];
    strcpy(cadena_, c.cadena_);    
    c.tam_=0;
    delete[] c.cadena_;
    c.cadena_ = new char[1];
    c.cadena_ = "";
}

The declaration in usuario.h are:

//........
public:
     Cadena id() const { return id_; }
//....
private:
     Cadena direccion_;

The class Cadena is work perfectly, but now i need use it and the compiler drop those errors. I think that i don´t doing nothing special, only return/assign a object of type Cadena..

I expect yours feedback,

best regards.

Это было полезно?

Решение

Just for completeness, the Standard mandates the behavior from your compiler warning:

12.8 Copying and moving class objects [class.copy]

7 If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

So if you want copying behavior, you will have to provide a copy constructor and copy assignment operator yourself.

However, looking at your move constructor, it appears that this doesn't provide any move semantics but rather deep copying (hint: what does strcopy() do? it sure doesn't move). So I would rename your current move constructor to the copy constructor, and provide a new move constructor that actually moves (i.e. re-assigns the char pointer or whatever is the handle to the actual data in your class).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top