当我尝试将给定大小的vector类的成员方法专门化时,会出现问题.
我可以提供一个简单的例子:
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 declarationsee 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 >::cross< TestVector< 3,Type > > (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
我在这里错过了什么?
谢谢,
弗洛朗
template<size_t S,typename T>class Vec { // ... stuff frIEnd struct Cross<S,T>; Vec<S,T> cross(const Vec<S,T>& other) { return Cross<S,T>()(*this,other); } // ... more stuff};template<size_t S,typename T> struct Cross { Vec<S,T> operator() (const Vec<S,T>& a,const Vec<S,T>& b) { // general deFinition }};// Partial specializationtemplate<typename T> struct Cross<3,T> { vec<3,T> operator() (const Vec<3,const Vec<3,T>& b) { // specialize deFinition }};总结
以上是内存溢出为你收集整理的c – 模板部分特化问题全部内容,希望文章能够帮你解决c – 模板部分特化问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)