简体   繁体   中英

Constructor copy, list, pointers and template in C++

i would like to do a copy constructor and define the = operator for this following class

template <class S, class T>
class Graphe
{
protected:
int prochaineClef;
public:

PElement< Sommet<T> > * lSommets; // liste de sommets
PElement< Arete<S,T> > * lAretes; // liste d'arêtes

Graphe(const Graphe<S,T> & graphe);
const Graphe<S,T> & operator = (const Graphe<S,T> & graphe);
}

So far i've tried this about the constructor :

template <class S, class T>
Graphe<S,T>::Graphe(const Graphe<S,T> & graphe)
{
PElement< Sommet<T> > * nouvelListeSommet = new PElement<Sommet<T>>(*graphe.lSommets);
PElement< Arete<S,T> > * nouvelListeAretes = new PElement<Arete<S,T>>(*graphe.lAretes);
this->prochaineClef = graphe.prochaineClef;
this->lAretes = nouvelListelAretes;
this->lSommets = nouvelListeSommet;

//nouvelListeSommet = graphe.lSommets->copieListe(graphe.lSommets);
//nouvelListelAretes = graphe.lAretes->copieListe(graphe.lAretes);

}

So i got this error saying 
\visual studio 2012\projects\ihm\tp2graphe\tp2graphe\pelement.h(123): error C2664: 'PElement<T>::PElement(T *,PElement<T> *)' : can't convert param1 from 'PElement<T> *const ' to 'Sommet<T> *'
1>          with
1>          [
1>              T=Sommet<InfoSommetCarte>
1>          ]
1>          and
1>          [
1>              T=Sommet<InfoSommetCarte>
1>          ]
1>          and
1>          [
1>              T=InfoSommetCarte
1>          ]

Here is my PElement class :

class PElement
{
public :
T * v;
PElement<T> * s;


PElement( T * v, PElement<T> * s );
PElement(PElement<T> & l);
}

template<class T>
PElement<T>::PElement(PElement<T> & l)
{
    //this->v = new T(l->v);
    this = new PElement<T>(l,this);
}

i don't know how to fix my copy constructor PElement is this->v = new T(l->v) correct?

Here is my wrong copieListe method :

/*
template<class T>
PElement<T> * PElement<T>::copieListe(PElement<T> * original)
{
    for(int i = 0; i < PElement<T>::taille(original);i++)
    {
        this->insertionTete(original->v,this);
        original = original->s;
    }
    return this;
}
*/

The expression graphe.lSommets is a pointer and you don't have a PElement constructor taking a pointer.

This can be solved by either make a new constructor that takes a pointer, or (the way I recommend) dereferencing the pointer:

new PElement<Sommet<T>>(*graphe.lSommets);
//                      ^
//                      |
// Note the dereference operator

You need a copy initializer for the PElement class that copies the next element if the list is not empty.

template <class T>
class PElement
{
public :
    T * v;
    PElement<T> * s;

    PElement(T * v, PElement<T> * s = nullptr);
    PElement(const PElement<T> & l);
};

template<class T>
PElement<T>::PElement(T * v, PElement<T> * s) : v(v), s(s) {}

template<class T>
PElement<T>::PElement(const PElement<T> & l)
{
    v = new T(*l.v);
    s = l.s ? new PElement<T>(*l.s) : nullptr;
}

This is then the copy initializer of the graph class

template <class S, class T>
Graphe<S,T>::Graphe(const Graphe<S,T> & graphe)
{
    lSommets = graphe.lSommets ? new PElement<Sommet<T>>(*graphe.lSommets) : nullptr;
    lAretes = graphe.lAretes ? new PElement<Arete<S,T>>(*graphe.lAretes) : nullptr;
    prochaineClef = graphe.prochaineClef;
}

I wouldn't implement it in this way because the list is copied recursively and you may get a stack overflow if the list is very long. As suggested by @Rerito you should use std::list instead. This code example shows you how you should have done it to avoid the error you got.

The copy constructor of PElement expects a reference on a PElement. This is why we pass *graphe.lSommets and *graphe.lAretes.

The following copy constructor for PElement<T> should work.

template <typename T>
PElement<T>::PElement(const PElement<T> &o) : v(nullptr), s(nullptr) {
    // I assume you are marking the end of the list by a nullptr sentinel
    if (nullptr != o.s) {
        s = new PElement<T>(*o.s);
    }
    // Two options for `v`, pick one...
    v = o.v; // Shallow copy of the T pointers
    v = new T(*o.v) // Deep copy of the T pointers... Assume T is copyable
}

It will recursively copy each element in your list and stop when hitting the nullptr sentinel.

If you want to keep PElement the way it is (using T* to hold the template type stuff), you may want to perform a deep copy (thus you pick this->v = new T(*ov) ), otherwise you might consider storing by value in the list elements.

You can then use it in your graph copy constructor:

template <typename S, typename T>
Graphe<S,T>::Graphe(const Graphe<S,T> &g) {
    lSommets = new PElement<Sommet<T>>(*g.lSommets);
    lAretes = new PElements<Arete<S,T>>(*g.lAretes);
    // ... Whatever work you need ...
    prochaineClef = g.prochaineClef;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM