2011-01-21 20 views
1

明示的な特化、C++

template <class U> 
class List 
{ 
    public: 
     virtual void clear(); 

}; 


template <class T> 
template <> 
void List < Car <T> >::clear() //Specialization U = Car <T>, compiler error 
{ 
    .... 
} 

クラスの車:

template <class T> 
class Car 
{ 
    T speed; 
    ... 
} 

コンパイルエラー:

エラー16エラーC3855: 'リスト':テンプレートパラメータ '車' は、宣言さhと互換性がありません... \一覧.hpp 75 エラー20エラーC2264: 'List :: clear':関数定義または宣言でエラーが発生しました。時間呼び出されていない機能:... \ List.hpp 75

しかし、この構造は、あなたが軌道に乗る必要があります

template <> 
void List < Car <double> >::clear() //Specialization U = Car <T>, compiler error 
{ 
    .... 
} 
+1

'template <>'行なしで動作しますか? – fredoverflow

+0

私はあなたが実際には明示的な専門化List >を定義する必要があると思います。それは私のコンパイラが不満を言っているからです: "不完全な型の無効な使用 'struct List >" – BatchyX

+0

btw –

答えて

0

This faq item OKです。

template<class T> 
class Car 
{ 
}; 

template <class U> 
class List 
{ 
    public: 
     virtual void clear(); 

}; 

template <class T> 
class List<Car<T> > 
{ 
    public: 
     virtual void clear() { /* specialization */ } 
}; 

または、非インラインバージョン:

template <class T> 
class List<Car<T> > 
{ 
    public: 
     virtual void clear(); 
}; 

template <class T> 
void List<Car<T> >::clear() { 
    /* specialization */ 
} 

以来、私はあなたがこれを行うことができる唯一の方法だと思います

struct Car 
{ 
}; 

template <> 
void List <Car>::clear() //Specialization U = Car <T>, compiler error 
{ 
    .... 
} 
2

は、従って、このようなものですあなたは実際にList<T>を専門に扱っているわけではありませんが、部分的にそれを専門にしています。 eタイプは引き続き表示されます。とにかく私の控除は間違っている可能性があります。

関連する問題