【发布时间】:2015-03-17 19:25:30
【问题描述】:
好的,关于 c++ 模板的新问题...
我正在为 2d/3d/4d 向量(如几何向量,而不是数组)编写模板类。 一切都很好,在 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 获得 我尝试在名称空间内、在其外、在其外以全分辨率(包括 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>'
【问题讨论】:
-
我怀疑错误输出中的那一行是唯一的,请编辑您的问题以包含 complete 和 unedited 错误输出。还请显示您用于调用函数的代码,包括所涉及的变量的声明。
-
请编辑问题。
-
@Axalo 然后就更奇怪了。 gcc 不喜欢该代码有什么问题?
-
@JoachimPileborg 如果您点击我在问题中提供的链接,您可以看到代码以及完整的错误消息