Error C2558 - Copy Designer

When I compile my class, I get a serious error:

C2558 No copy constructor or copy constructor declared explicit

But my copy constructor is neither private nor explicit!

Title:

#include "Csequence.h" using namespace std; class Cnoeud { private: Cnoeud *oNOEpere; vector<Cnoeud> oNOEfils; Csequence oNOEsequence; bool oNOEStatut; public: // Liste des constructeurs Cnoeud(); Cnoeud(Cnoeud &); ~Cnoeud(){} // Liste des accesseurs et des modificateurs Cnoeud * NOEAfficherpere (){ return oNOEpere;} vector<Cnoeud> NOEAfficherfils() {return oNOEfils;} Csequence NOEAffichersequence() {return oNOEsequence;} bool NOEAfficherstatut() { return oNOEStatut;} void NOEModifierpere(Cnoeud oNOEp){ *oNOEpere=oNOEp;} void NOEModifierfils(vector<Cnoeud>); void NOEModifiersequence(Csequence oNOEs){oNOEsequence = oNOEs;} void NOEModifierstatut(bool {oNOEStatut = b;} // Liste des fonctions membres void NOEViderfils(){ oNOEfils.clear();} // Surcharge de l'opΓ©rateur d'affectation Cnoeud & operator=(Cnoeud &) ; }; 

A source:

 Cnoeud.cpp #include <iostream> #include <vector> #include "Cnoeud.h" using namespace std; Cnoeud::Cnoeud() { oNOEStatut= 0; oNOEsequence.SEQInitialiserdatesfin(); oNOEsequence.SEQInitialisersequence(); oNOEpere = NULL; } Cnoeud::Cnoeud(Cnoeud & oNOE) { oNOEStatut= oNOE.oNOEStatut; oNOEsequence = oNOE.NOEAffichersequence(); oNOEpere = oNOE.NOEAfficherpere(); oNOEfils.clear(); vector<Cnoeud>::iterator it; for(it=oNOE.NOEAfficherfils().begin();it!=oNOE.NOEAfficherfils().end();it++) { oNOEfils.push_back(*it); } } 
+4
source share
3 answers

That Cnoeud(Cnoeud &) not a copy constructor, as expected by most code using this class. Copy constructor must be

 Cnoeud(const Cnoeud &); 

And why don't you make this argument const ? The copy constructor, of course, should not modify the object from which it is copied. Please note that this also applies to your assignment statement. In addition, these accessors must be const member functions:

 Cnoeud * NOEAfficherpere () const { return oNOEpere;} // ^^^^^ 

See this answer about using const .

+18
source

The copy constructor accepts a constant reference, i.e.

Cnoeud::Cnoeud(const Cnoeud& oNOE)

+3
source

Cnoeud( Cnoeud & ) not a regular signature for a copy constructor, although it is sometimes used to "transfer ownership" when copying, i.e. it moves something from the origin to a new object. This is usually used to get around problems that the object is about to "clean up" after use, when you do not want to use reference counting pointers.

With C ++ 0x there will be semantics of moving with r-values, where you can do Cnoeud( Cnoeud && ) to "move" an object that is not copied, but can be returned from a function.

It is unsafe to use objects with transfer-ownership semantics (e.g. auto_ptr) in collections such as a vector. You are lucky in a sense that you are caught in a compiler error, because it relieved you of pain during the execution of the error - it is much more difficult to find the problem.

If you want to create the correct constructor copy, then pass the parameter as a reference to the constant (as others have suggested).

By the way, in your "copy constructor" it turns out that

 oNOEfils.clear(); vector<Cnoeud>::iterator it; for(it=oNOE.NOEAfficherfils().begin();it!=oNOE.NOEAfficherfils().end();it++) { oNOEfils.push_back(*it); } 

easy to replace with

oNOEfils = oNOE.NOEAfficherfils();

More efficient recording (single line) and almost certainly more efficient to run.

+2
source

Source: https://habr.com/ru/post/1335384/


All Articles