我正在尝试编写一个数学编程通用大小和类型向量类。我在部分特化方面遇到了问题。
当我尝试为给定的大小专门设计向量类成员方法时,问题就出现了。
下面是一个简单的例子:
尝试编译这个简单的示例时,我遇到了编译错误,指出“cross”特化不符合现有声明:
当我尝试为给定的大小专门设计向量类成员方法时,问题就出现了。
下面是一个简单的例子:
template <size_t Size, typename Type>
class TestVector
{
public:
inline TestVector (){}
TestVector cross (TestVector const& other) const;
};
template < typename Type >
inline TestVector< 3, Type > TestVector< 3, Type >::cross (TestVector< 3, Type > const& other) const
{
return TestVector< 3, Type >();
}
void test ()
{
TestVector< 3, double > vec0;
TestVector< 3, double > vec1;
vec0.cross(vec1);
}
尝试编译这个简单的示例时,我遇到了编译错误,指出“cross”特化不符合现有声明:
error C2244: 'TestVector<Size,Type>::cross' : unable to match function definition to an existing declaration
see declaration of 'TestVector<Size,Type>::cross'
definition
'TestVector<3,Type> TestVector<3,Type>::cross(const TestVector<3,Type> &) const'
existing declarations
'TestVector<Size,Type> TestVector<Size,Type>::cross(const TestVector<Size,Type> &) const'
我尝试声明cross为一个模板:
template <size_t Size, typename Type>
class TestVector
{
public:
inline TestVector (){}
template < class OtherVec >
TestVector cross (OtherVec const& other) const;
};
template < typename Type >
TestVector< 3, Type > TestVector< 3, Type >::cross< TestVector< 3, Type > > (TestVector< 3, Type > const& other) const
{
return TestVector< 3, Type >();
}
这个版本在编译时通过,但在链接时失败:
unresolved external symbol "public: class TestVector<3,double> __thiscall TestVector<3,double>::cross<class TestVector<3,double> >(class TestVector<3,double> const &)const
我这里缺少什么吗? 谢谢, Florent
class
,而是在其中某个函数上进行了特化,这里的class
是模板化的,因此部分特化应该是另一个类模板。 - Tony The Lion