GCC模板错误

时间:2012-04-09 16:41:33

标签: c++ templates gcc

我正在尝试将C ++项目移植到iOS。它在Linux和Windows以及MSVC上的QtCreator中编译得很好。 现在在Xcode / GCC上,有一个模板化的类我得到以下错误:“错误:模板参数列表太少了。”

导致此错误的代码如下所示:

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对标准更严格。 任何人都可以告诉我究竟是什么问题以及如何解决它?

2 个答案:

答案 0 :(得分:4)

当您定义相应类的方法的完全特化时,您仍然需要在定义前添加template <>,这是您缺少的“模板参数列表”。此外,构造函数必须以类的名称命名,因此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;
}

答案 1 :(得分:3)

这只是猜测,但您是否需要添加模板&lt;&gt;?

由于它是模板专业化,我相信它仍然需要成为模板。

离。

template<>
MyFloatIntClass::MyClassImpl() {}

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

编辑:从模型的回答 - 结果证明它需要ctor的无类型名称。

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;
}
相关问题