0

C++プロジェクトをiOSに移植しようとしています。LinuxとWindowsのQtCreator、およびMSVCでも問題なくコンパイルされます。Xcode / GCCで、特定のテンプレートクラスを使用すると、次のエラーが発生します:「エラー:template-parameter-listsが少なすぎます」。

このエラーの原因となるコードは次のようになります。

template <typename TA, typename TB, int Type>
class MyClassImpl
{
public:
   MyClassImpl();
   virtual int GetType() const
   {
       return type;
   }
};    

typedef MyClassImpl<float, int, 12> MyFloatIntClass;

MyFloatIntClass::MyFloatIntClass()
{
...
}

int MyFloatIntClass::GetType() const
{
   return 22;
}

typedef構文に関する何かが違法であり、GCCは標準に対してより厳格であると私は推測しています。誰かが私に問題が正確に何であるか、そして私がそれをどのように修正することができるか教えてもらえますか?

4

2 に答える 2

4

それぞれのクラスのメソッドの完全な特殊化を定義しているので、定義の前に、template <>欠落している「template-parameter-lists」である。を付ける必要があります。さらに、コンストラクターはクラスと同じように名前を付ける必要があるため、MyFloatIntClass::MyFloatIntClass()違法です(MyFloatIntClassクラス名ではなく、単なるエイリアスです)。以下は私にとってはうまくコンパイルされます(g ++ 4.5.3):

template <typename TA, typename TB, int Type>
class MyClassImpl
{
public:
   MyClassImpl();
   virtual int GetType() const
   {
       return Type;
   }
};    

typedef MyClassImpl<float, int, 12> MyFloatIntClass;

template <>
MyFloatIntClass::MyClassImpl()
{
}

template <>
int MyFloatIntClass::GetType() const
{
   return 22;
}
于 2012-04-09T16:52:22.260 に答える
3

これは単なる推測ですが、テンプレートを追加する必要があります<>?

テンプレートの特殊化なので、まだテンプレートである必要があると思います。

元。

template<>
MyFloatIntClass::MyClassImpl() {}

template<>
int MyFloatIntClass::GetType() const {
    return 22;
}

編集:modelnineの答えから-ctorのuntypedefされた名前が必要であることがわかりました。

EDIT2:次のコードは私にとってはうまく機能します:

template <typename TA, typename TB, int Type>
class MyClassImpl
{
public:
   MyClassImpl();
   MyClassImpl(const MyClassImpl<TA, TB, Type>&);
   virtual int GetType() const
   {
       return Type;
   }
   const MyClassImpl<TA, TB, Type>& operator=(const MyClassImpl<TA, TB, Type>&);
};    

typedef MyClassImpl<float, int, 12> MyFloatIntClass;

template<>
MyFloatIntClass::MyClassImpl()
{
 //
}

template<>
MyFloatIntClass::MyClassImpl( const MyFloatIntClass& rhs )
{
 //
}

template<>
const MyFloatIntClass& MyFloatIntClass::operator=( const MyFloatIntClass& rhs )
{
  return *this;
}

template<>
int MyFloatIntClass::GetType() const
{
   return 22;
}
于 2012-04-09T16:47:24.980 に答える