2015-09-07 103 views
1

我有這個類:模板和typedef的錯誤

template <class T> 
class NodoLista 
{ 
public: 
    T dato; 
    Puntero<NodoLista<T>> sig; 
    NodoLista<T>(const T& e, Puntero<NodoLista<T>> s) : dato(e), sig(s) { }; 
}; 

然後我嘗試使用該類型定義是這樣的:

template <class U> 
typedef Puntero<NodoLista<U>> pNodoLista; 
void main() 
{ 
    pNodoLista<int> nodo = new NodoLista<int>(1, nullptr); 
    cout<<nodo->dato<<endl; 
} 

,我得到一個錯誤,說我的模板不正確。 如何使用typedef的使用方法:

Puntero<NodoLista<T>> as pNodoLista<T> 
+0

我試過這個話題,它沒有奏效。這就是爲什麼我打開一個新的 –

回答

0

嘗試使用

template <class T> 
using pNodoLista = Puntero<NodoLista<T>>; 

現在pNodoLista<T>相當於Puntero<NodoLista<T>>

LIVE

如果你的編譯器不支持C++ 11,你可以用一種變通方法:

template <class T> 
struct pNodoLista 
{ 
    typedef Puntero<NodoLista<T>> type; 
}; 

現在pNodoLista<T>::type相當於Puntero<NodoLista<T>>

LIVE

BTW:main()應該返回int

+0

我得到語法錯誤'使用'和模板聲明不能正確解析 –

+0

@FaustoSanchez什麼是編譯器你使用了嗎?我已經添加了一個實時演示。http://ideone.com/SU9wzq – songyuanyao

+0

我不知道如何告訴你我使用的是什麼樣的相似物。我該怎麼做? –

2
template <class U> 
typedef Puntero<NodoLista<U>> pNodoLista; 

應該

typedef template <class U> Puntero<NodoLista<U>> pNodoLista; 
+0

那沒有工作的人,我得到了一個模板上的錯誤 –

+0

特別是哪個錯誤?請提高你的問題! –

+0

我得到這個錯誤:「語法錯誤'模板<',未聲明的標識符U –