c – 模板部分特化问题 |
您所在的位置:网站首页 › c2244错误 › c – 模板部分特化问题 |
概述我正在尝试为数学编程编写一个大小和类型泛型向量类.我遇到部分专业化的问题. 当我尝试将给定大小的vector类的成员方法专门化时,会出现问题. 我可以提供一个简单的例子: template class TestVector{public: inline TestVector (){} TestVector cross ( 我正在尝试为数学编程编写一个大小和类型泛型向量类.我遇到部分专业化的问题. 当我尝试将给定大小的vector类的成员方法专门化时,会出现问题. 我可以提供一个简单的例子: template 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::cross' : unable to match function deFinition to an existing declarationsee declaration of 'TestVector::cross'deFinition 'TestVector TestVector::cross(const TestVector &) const' existing declarations 'TestVector TestVector::cross(const TestVector &) const'我试图将cross声明为模板: template 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 __thiscall TestVector::cross(class TestVector const &)const我在这里错过了什么?谢谢,弗洛朗 解决方法 一种方法是将cross定义为“functor”(即带有operator()的类). templateclass Vec { // ... stuff frIEnd struct Cross; Vec cross(const Vec& other) { return Cross()(*this,other); } // ... more stuff};template struct Cross { Vec operator() (const Vec& a,const Vec& b) { // general deFinition }};// Partial specializationtemplate struct Cross { vec operator() (const Vec |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |