2012-01-04 41 views
2

テンプレートクラスの静的constメンバ変数を初期化しようとすると、奇妙な問題が発生します。私の他の静的変数はすべて正常に初期化されますが、何らかの理由でこの変数が嫌いです。私はいくつかのサンプルコードをまとめてテストしています。問題はないので、実際に何が起こっているのか分かりません。C++静的constテンプレートメンバの初期化

これに加えて、テンプレートクラス内で宣言されたtypedefを使用する関数の定義に問題があります。同じ問題で型を見つけることができないということです。この問題は私が以下のコードで再現することができました。私はそれを修正する一つの方法は、クラス内の関数を定義することです知っているが、関数が本当に大きいと私はクラスの定義を容易にするために、クラスの外で定義されたすべての巨大な関数読む。その後、私は例外を作成する必要がありますねかかわらず、それが私の唯一の選択肢だ場合...

class tTestType 
{ 
    public: 

     tTestType(int32_t val) : fValue(val) { } 

    private: 

     int32_t fValue; 
}; 

template<class T> 
class tTestTemplate 
{ 
    public: 

     tTestTemplate() { } 

    private: 

     typedef std::vector<int32_t> tSomeVec; 

     tSomeVec mTestFunction() const; 

     static const tTestType kTestStatic; 
}; 

// Should cause the following errors but I can't reproduce them for some reason: 
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
// error C2988: unrecognizable template declaration/definition 
// error C2059: syntax error : 'constant' 
template<class T> 
const tTestType tTestTemplate<T>::kTestStatic(10); 

// Causes the following errors: 
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction' 
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
// fatal error C1903: unable to recover from previous error(s); stopping compilation 
template<class T> 
tTestTemplate<T>::tSomeVec tTestTemplate<T>::mTestFunction() const 
{ 
    tSomeVec result; 
    result.push_back(0); 
    return result; 
} 

答えて

0

私は両方の問題の解決策を見つけました。

静的メンバー変数の最初の問題については、定義をCPPファイルに移動し、テンプレートの特殊化を使用しました。これは、私が投稿したテストコードで壊れない理由は、基本的な型(int、floatなど)が問題を処理するためですが、クラスのようなより複雑な型を使用するとエラーが発生するはずです。この解決法は、世界で最も良いことではありませんが、それはややきれいです。誰かがよりよい解決策を持っている場合は私に知らせてください。第二の問題については

template<> 
const tTestType tTestTemplate<uint32_t>::kTestStatic(10); 

、クラス内で宣言された型を使用する関数が、私は私が最初の記事で説明した溶液を用いて行って、ちょうど関数定義を移動しましたテンプレートクラスの内部では次のようになります:

template<class T> 
class tTestTemplate 
{ 
    public: 

     tTestTemplate() { } 

    private: 

     typedef std::vector<int32_t> tSomeVec; 

     // Declaring the function inside the class to fix the compiler error. 
     tSomeVec mTestFunction() const 
     { 
      tSomeVec result; 
      result.push_back(0); 
      return result; 
     } 

     static const tTestType kTestStatic; 
}; 
-1

は、私はあなたのコードとそのコンパイル罰金で2つの変更を行いました。

class tTestType 
{ 
    public: 

     tTestType(int32_t val) : fValue(val) { } 

    private: 

     int32_t fValue; 
}; 

typedef std::vector<int32_t> tSomeVec; 

template<class T> 
class tTestTemplate 
{ 
    public: 

     tTestTemplate() { } 

    private: 

     tSomeVec mTestFunction() const; 

     static const tTestType kTestStatic; 
}; 

// Should cause the following errors but I can't reproduce them for some reason: 
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
// error C2988: unrecognizable template declaration/definition 
// error C2059: syntax error : 'constant' 
template<class T> 
const tTestType tTestTemplate<T>::kTestStatic(10); 

// Causes the following errors: 
// error C2143: syntax error : missing ';' before 'tTestTemplate<T>::mTestFunction' 
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
// fatal error C1903: unable to recover from previous error(s); stopping compilation 
template<class T> 
tSomeVec tTestTemplate<T>::mTestFunction() const 
{ 
    tSomeVec result; 
    result.push_back(0); 
    return result; 
} 
+1

これは「スポットの違い」パズルですか?あなたは何を変えましたか、なぜですか? – bk1e

+0

私はその場所が違いのパズルだとは思わない。 mTestFunction()の定義方法を変更しました。 – Raghuram

+0

これで、クラス外でtypedefを移動しましたか?あなたはちょうどそれを言ったかもしれません。私はそれが別の解決策であると思います。考慮すべき何か、ありがとう。私はまだ別の問題があり、私が言ったように、サンプルコードには問題がありません。静的メンバー宣言に関するコメントで指定したエラーの原因を知っている人がいるかどうかを調べたいだけです。 – Shenjoku

関連する問題