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

标签 c++ templates

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

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

所以,错误是:

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 获得。 我试过在 namespace 内、在它外面、在它外面以完整的分辨率(包括 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 - 的第二个非类型模板参数的类型>,而类 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 )
{
    // ...
}

关于c++模板运算符未找到匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030573/

相关文章:

c++ - 是否可以指定模板类的两个部分? ( '<' 之前和 '< >' 内部的部分)

c++ - 使用 reinterpret_cast 和 static_cast 模拟具有多态参数的模板中的协变和逆变?

c++ - 简单参数包扩展 : expected ';'

WordPress 评论 child 定制与 bootstrap 一起使用?

c++ - 将一个值从 fortran 传输到 C++,以便它通过共享内存在 C++ 中显示为 numeric_limits<float>

c++ - g++ 在 C++ 中的链接错误

c++ - 如何使用 boost::date_time 获取两个日期之间的天数

c++ - += C++ 中的运算符,用于将作者添加到列表

C++:标签/特征模板:确保标签只绑定(bind)一次

C++ 如何将成员函数指针传递给另一个类?