2016-02-28 84 views
0

我在C++中遇到了模板問題。使用模板C++調用'...'時沒有匹配,OOP

這是代碼:

//main.cpp 
main(){ 
    gestione_bagagli bag; 
    Data da(10,8,1980); 
    Proprietario pr("Luca","Verdi",da,"Este",true); 
    Viaggio vi("12345","Roma"); 
    speciale b(20,30,48,30,pr,vi); 
    speciale& c=b; 
    bag.lug.push_back(b); 
} 
//gestione_bagagli.h 
class gestione_bagagli{ 
    friend std::ostream& operator <<(std::ostream&,const gestione_bagagli&); 
public: 
    Lista<bagaglio*> lug; 
    gestione_bagagli(){} 
    template <class T> 
    gestione_bagagli(const Lista<T>&){} 
}; 
//contenitore.h 
template<class T> 
class Lista{ 
friend class iteratore; 
friend std::ostream& operator<< <T>(std::ostream&, const Lista<T>&); 
private: 
    class nodo{ 
    public: 
     nodo(){} 
     nodo(const T& bag, nodo* p, nodo* n): b(bag),prev(p),next(n){} 
     T b; 
     nodo* prev; 
     nodo* next; 
    }; 
    int n_el; 
    nodo* first, *last; 
public: 
    Lista():first(0),last(0),n_el(0){} 
    void push_back(const T& b){ 
     if(first && last){ 
      last->next=new nodo(b,last,0); 
      last=last->next; 
     }else first=last=new nodo(b,0,0); 
     n_el++; 
    } 
}; 

的問題是在主在bag.lug.push_back(B); speciale是一個派生類型,但問題在於模板的istantation。 錯誤是「main.cpp:14:錯誤:沒有匹配函數調用'Lista :: push_back(speciale &)'」,其中bagaglio是層次結構的基類。

我知道,我需要明確定義函數模板,但它不起作用! 我tryed這一點:

bag.lug.push_back <bagaglio*>(b); 

不過是一個sintax錯誤

回答

0

你推一個specialebagaglio*列表。除非speciale有一個轉換運算符來指向bagaglio,這不會發生。

也許你想推一個指針到speciale

bag.lug.push_back(&b); //notice the address-of operator 
+0

我覺得很蠢,你說得對。謝謝! – CodeBott