Hi, When I compile my class, I find a serious error C2558
no copy constructor available or copy constructor is declared 'explicit'
My copy constructor is not private and not explicit ! I don't have any idea about this error
Help me please.


My class is :

Cnoeud.h

#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 &) ;


};

# endif

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 = oNOEpere;
oNOEfils.clear();
vector<Cnoeud>::iterator it;
for(it=oNOE.NOEAfficherfils().begin();it!=oNOE.NOEAfficherfils().end();it++)
{
oNOEfils.push_back(*it);
}
}

The correct copy contructor is :

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);
    }
}

But I can't fix this error :s

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Actually correct Copy constructor is
MyClass::MyClass(const MyClass& rhs);

So in your case:
Cnoeud::Cnoeud(const Cnoeud & oNOE)
and you MUST not change oNOE in the constructor. So in that case you must use const_iterator to iterate.

This article has been dead for over six months. Start a new discussion instead.