テンプレートクラスの静的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;
}
これは「スポットの違い」パズルですか?あなたは何を変えましたか、なぜですか? – bk1e
私はその場所が違いのパズルだとは思わない。 mTestFunction()の定義方法を変更しました。 – Raghuram
これで、クラス外でtypedefを移動しましたか?あなたはちょうどそれを言ったかもしれません。私はそれが別の解決策であると思います。考慮すべき何か、ありがとう。私はまだ別の問題があり、私が言ったように、サンプルコードには問題がありません。静的メンバー宣言に関するコメントで指定したエラーの原因を知っている人がいるかどうかを調べたいだけです。 – Shenjoku