c ++模板运算符未找到匹配项

c++ template operator no match found

好的,关于 C++ 模板的新问题...

我正在为 2d/3d/4d 向量(如几何向量,而不是数组)编写模板 class。 一切都很好,在 SO 中提出了一系列问题之后,但由于某种原因现在找不到运营商。 如果我在 class 内声明它们,没问题,但如果我在外部将它们声明为模板,则找不到它们。 有趣的是,如果我用正确的变量类型专门声明它们,那么一切都会好起来的。所以基本上看起来好像该函数模板从未实例化过。

所以,错误是:

error: no match for ‘operator-’ (operand types are ‘Math::TVector<int, 3ul>’ and ‘Math::TVector<int, 3ul>’)

尽管它有一个功能:

template <typename Type, unsigned TemplateElementCount>
Math::TVector <Type,TemplateElementCount> operator - ( Math::TVector <Type,TemplateElementCount> &First, Math::TVector <Type,TemplateElementCount> &Second )
{
Math::TVector <Type,TemplateElementCount> Result;
for ( unsigned cont = 0; cont < TemplateElementCount; ++cont )
    Result.Data[cont] = First.Data[cont] - Second.Data[cont];
return Result;
}

可在 http://goo.gl/qrZaU1 获取代码示例 我试过在名称空间内、在它外面、在它外面以完整的分辨率(包括 Math:: everywhere )声明它,但没有任何效果。 谁能帮我一把? 谢谢

编辑: 完整的编译错误是

main.cpp: In function 'int main(int, char**)':                                                                                  
main.cpp:16:23: error: no match for 'operator-' (operand types are 'Math::TVector<int, 3ul>' and 'Math::TVector<int, 3ul>')     
 Vector1 = Vector1 - Vector2;                                                                                               
                   ^                                                                                                        
main.cpp:16:23: note: candidate is:                                                                                             
In file included from main.cpp:2:0:                                                                                             
Point.h:171:43: note: template<class Type, unsigned int TemplateElementCount> Math::TVector<Type, TemplateElementCount> operator
-(Math::TVector<Type, TemplateElementCount>&, Math::TVector<Type, TemplateElementCount>&)                                       
 Math::TVector <Type,TemplateElementCount> operator - ( Math::TVector <Type,TemplateElementCount> &First, Math::TVector <Type,Te
mplateElementCount> &Second )                                                                                                   
                                       ^                                                                                    
Point.h:171:43: note:   template argument deduction/substitution failed:                                                        
main.cpp:16:25: note:   mismatched types 'unsigned int' and '#'integer_cst' not supported by dump_type#<type error>'            
 Vector1 = Vector1 - Vector2;                                                                                               
                     ^                                                                                                      
main.cpp:16:25: note:   'Math::TVector<int, 3ul>' is not derived from 'Math::TVector<Type, TemplateElementCount>' 

问题(或者至少是其中一个问题)似乎是您使用 unsigned 作为 operator - 的第二个非类型模板参数的类型,而 class TVector 使用类型为 std::size_t 的相应非类型模板参数实例化。这两种类型不一定相同(根据您收到的编译器错误,似乎 std::size_t 在您的平台上解析为 unsigned long),因此出现错误。

按如下方式更改函数的签名应该可以解决问题:

template <typename Type, std::size_t TemplateElementCount>
//                       ^^^^^^^^^^^
Math::TVector <Type,TemplateElementCount> operator - (
    Math::TVector <Type,TemplateElementCount> &First, 
    Math::TVector <Type,TemplateElementCount> &Second )
{
    // ...
}