2016-09-24 6 views
0
#pragma once 
//includes 
template<class RefType> 
class Foo 
{ 
public: 

template<> 
enum Foo<QString>::bar { //values A }; //LINE X 
template<> 
enum Foo<double>::bar { //values B }; 
template<> 
enum Foo<Kraken::Point3>::bar { //values C }; 
//functions 
}; //LINE Y 

コンパイラがあれば、私はこのエラーの原因を理解していないトラブル: 'クラス' タイプのredifinitionは

note: see declaration of 'Foo<QString>' LINE X 
note: note: see reference to class template instantiation 'Foo<RefType>'LINE Y 

リアクションLINE X

error C2011: 'Foo<QString>': 'class' type redefinition 

のエラーを与えます私は問題にもっと啓発されるようになります。質問を明確にするように再フォーマットします

+0

あなたのコードで 'foo'と' bar'とは何ですか? – xinaiz

+0

クラス名(Foo)と列挙名(bar)のスタンド – brettmichaelgreen

+0

タイトルの「エピソード44」は何を意味していますか? – tambre

答えて

0

選択したタイプのためferent enum年代、あなたはこれらの型を使用して、テンプレートクラスを特化すべきである:あなたがクラステンプレートのほんの一部のメンバーを専門としたいよう

template<class RefType> 
class Foo 
{ 
    //default enum if you want 
}; 

template<> 
class Foo<QString> 
{ 
    enum bar {Q1, Q2, Q3};  
}; 

template<> 
class Foo<double> 
{ 
    enum bar {d1, d2, d3};  
}; 

template<> 
class Foo<Kraken::Point3> 
{ 
    enum bar {K1, K2, K3};  
}; 

あなたのコードが見えますが、それはC++では不可能です。

template<class Reftype> 
class FooImpl 
{ 
    RefType x; 
public: 
    void set_x(RefType val) {x=val;} 
    RefType get_x(void) {return x;} 
}; 

template<class RefType> 
class Foo : public FooImpl<Reftype> 
{ 

}; 

template<> 
class Foo<QString> : public FooImpl<QString> 
{ 
    enum bar {Q1, Q2, Q3};  
}; 

template<> 
class Foo<double> : public FooImpl<double> 
{ 
    enum bar {d1, d2, d3};  
}; 

template<> 
class Foo<Kraken::Point3> : public FooImpl<Kraken::Point3> 
{ 
    enum bar {K1, K2, K3};  
}; 

あなたが異なる列挙型を望んでいたという理由だけで、すべてのクラスのメンバーを再定義する必要はありません。この方法:クラス構造体のほとんどを維持しながら、これをハックする

一つの方法は、一般的なクラスの実装の公開継承であります専門分野で