2015-02-23 62 views
1

我願做一個拷貝構造函數和定義=運營商爲這個下面的類構造副本,列表指針和模板在C++

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

到目前爲止,我已經試過這大概構造:

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>   ] 

這裏是我的P成分類:

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

我不知道如何解決我的拷貝構造函數P成分 is this-> v = new T(l-> v)是否正確?

這裏是我的錯copieListe方法:

/* 
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; 
} 
*/ 
+0

在你要複製的P成分列表中的拷貝構造函數初始化圖形。 – chmike 2015-02-23 14:21:00

+0

準確完整地再現編譯器消息。標記與編譯器報告的行號對應的源代碼行。 – 2015-02-23 14:21:50

+0

你需要下面的代碼:lSommets = copieListe(graphe.lSommets);.然後您需要提供copieListe方法/函數。它返回一個PElement < Sommet> *,它是鏈接列表的副本。 – chmike 2015-02-23 14:29:10

回答

0

您需要爲P成分級副本初始化,下副本元素如果列表不是空的。

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

然後,這是該圖類的副本初始化

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

我不會實現它以這種方式,因爲名單是遞歸複製,你可能會得到一個堆棧溢出如果列表很長。正如@Rerito建議的,你應該使用std :: list來代替。這段代碼示例顯示了你應該怎麼做才能避免你得到的錯誤。

PElement的複製構造函數需要PElement上的引用。這就是爲什麼我們通過* graphe.lSommets和* graphe.lAretes。

+0

謝謝,它幫助了很多,但我仍然在「v = new T(l.v);」 - > C2664:無法將'Sommet * const'轉換爲'const Sommet &'。任何想法?是的@Rerito關於那個設計,我沒有做,我被迫與這些類工作,我希望我可以重做整個東西使用2 STL載體.. – rilent 2015-02-23 17:06:51

+0

我已經改變它與「v = new T(* lv );」我現在沒有得到任何錯誤,但它既沒有複製也沒有複製 – rilent 2015-02-23 17:13:34

+0

錯誤是「v = new T(l.v);」應該是「v = new T(* l.v);」以便可以使用複製初始值設定項。我會在我的答案的代碼中解決這個問題。 – chmike 2015-02-24 09:19:48

0

表達graphe.lSommets指針和你沒有PElement構造採用指針。

這可以通過解決要麼使一個新的構造函數指針或間接引用指針(我推薦的方式):

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

重做整個東西這不正確,因爲PElement是鏈表的一個元素。這會破壞鏈表。 – chmike 2015-02-23 14:23:48

0

以下PElement<T>的複製構造函數應該可以工作。

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 
} 

它將遞歸的每個元素在列表中擊中nullptr定點複製時停止。

如果你想保持PElement它(使用T*用來存放模板類型的東西)的方式,你可能要執行深拷貝(因此你挑this->v = new T(*o.v)),否則你可能會值列表考慮將元素。

然後,您可以在您的圖形拷貝構造函數使用它:

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