2016-03-21 15 views
0

Öğretmenim bir templete sınıf listesi yazmak istedi (sadece STL'de olduğu gibi) Kodumu derler ama çöker ama nedenini bulamıyorum, kimse bana yardım etmeye çalışabilir mi? Sanırım iki parametreli kurucuda sorun var mı? local NULL ise (local->getVal() NULL olup olmadığını test ediyoruz neden?)Yazma templete sınıfı Sorunlar alın

-----Main.cpp------ 
#include "Liste.h" 
#include <iostream> 

int main() 
{ 
    Liste<int> l ; 
    int x = 5 ; 
    int y = 6 ; 
    int z = 7 ; 
    l.insert_first(y) ; 
    l.insert_first(x) ; 
    l.insert_last(z) ; 
    std::cout << l << std::endl ; 
} 

-----Liste.h------ 
#pragma once 
#include <stdio.h> 
#include <iostream> 
#include "NoeudListe.h" 

template <class T> class Liste 
{ 

private: 
    NoeudListe<T>* tete; 
    NoeudListe<T>* fin; 

public: 

    Liste() 
    { 
     tete = new NoeudListe<T>(NULL,fin,NULL); //<-----I think the problem lies here beacause the val can`t be NULL? But I tried 0, still not work... 
     fin = new NoeudListe<T>(tete,NULL,NULL); 
    } 

    void insert_after (T& val, NoeudListe<T>* location) 
    { 
     NoeudListe<T>* local = new NoeudListe<T>(location,location->getNext(),val); 
     (*(location->getNext())).setPrev(local); 
     location->setNext(local); 
    } 

    void insert_first(T& val) 
    { 
     insert_after (val, tete); 
    } 

    void insert_last(T& val) 
    { 
     insert_after (val, fin->getPrev()); 
    } 

    friend std::ostream& operator << (std::ostream& os,const Liste<T>& location) 
    { 
     NoeudListe<T>* local=(location.tete)->getNext(); 
     while(local->getVal() != NULL) 
     { 
      os<< local->getVal() << "\n"; 
      local=local->getNext(); 
     } 
     return os; 
    } 
}; 

-----NoeudListe.h------ 
#pragma once 
template <class T> class NoeudListe 
{ 

private: 
    NoeudListe* prev; 
    NoeudListe* next; 
    T val; 

public: 

    NoeudListe(NoeudListe* p, NoeudListe* n, const T& v) 
    { 
     prev = p; 
     next = n; 
     val = v; 
    } 

    void setNext(NoeudListe* n){ next = n;} 
    void setPrev(NoeudListe* p){ prev = p;} 
    void setVal(const T& v){ val = v;} 

    NoeudListe* getNext(){ return next;} 
    NoeudListe* getPrev(){ return prev;} 
    T getVal(){ return val;} 

}; 
+0

Kodunuzu hata ayıklayıcısından geçirdiğinizde, çökmenin nedenini size anlattı? – Sneftel

+0

@Sneftel, gerçeği söylemek gerekirse, hala bir hata ayıklayıcısını nasıl kullanacağımı gerçekten bilmiyorum .... Ama hata ayıklayıcısını kullanarak, şunu elde ettim: bu = 0x41c30e <__ do_global_ctors + 46>, p = 0xb927f8 ve tete, fin, n mevcut değildir. Satırdan sonra: void setPrev (NoeudListe * p) {prev = p;}, –

+0

çökecektir Bu süreçte bir sonraki adımın ne olduğunu bildiğiniz gibi geliyor, sonra: bir hata ayıklayıcısını kullanmayı öğrenin. – Sneftel

cevap

0
friend std::ostream& operator << (std::ostream& os,const Liste<T>& location) 
{ 
    NoeudListe<T>* local=(location.tete)->getNext(); 
    while(local->getVal() != NULL) 
    { 
     os<< local->getVal() << "\n"; 
     local=local->getNext(); 
    } 
    return os; 
} 

Burada testi yok, buradan bir segfault alacak böylece.

+0

Merhaba kullanmayı öğrenmek için gidiyorum, Cevabınız için teşekkür ederiz. Dediğiniz gibi, yerel -> getVal() NULL ise, bilge test yapmıyor, bu yüzden 'while (local-> getNext()! = NULL)' olarak değiştirdim, yine de çökecek ve hata ayıklayıcı aynı şeyi veriyor. thing :( –

+0

Hata ayıklayıcı ne verir? –

+0

Hata ayıklayıcısını kullanarak bunu çalıştırdım: bu = 0x41c30e <__ do_global_ctors + 46>, p = 0xb927f8 ve tete, fin, n kullanılamıyor. Satırdan sonra: void setPrev (NoeudListe * p) {prev = p;}, –