c++;模板运算符未找到匹配项 >,关于C++模板……/P>的新问题
我正在为2d/3d/4d向量(如几何向量,而不是数组)编写一个模板类。 在这里问了一大堆问题之后,一切都很好,但由于某些原因,现在找不到运算符。 如果我在类中声明它们,这没关系,但是如果我在外部将它们声明为模板,则找不到它们。 有趣的是,如果我专门用正确的变量类型声明它们,那么一切都会好起来。所以基本上看起来函数模板从未被实例化过 因此,错误是:c++;模板运算符未找到匹配项 >,关于C++模板……/P>的新问题,c++,templates,C++,Templates,我正在为2d/3d/4d向量(如几何向量,而不是数组)编写一个模板类。 在这里问了一大堆问题之后,一切都很好,但由于某些原因,现在找不到运算符。 如果我在类中声明它们,这没关系,但是如果我在外部将它们声明为模板,则找不到它们。 有趣的是,如果我专门用正确的变量类型声明它们,那么一切都会好起来。所以基本上看起来函数模板从未被实例化过 因此,错误是: error: no match for ‘operator-’ (operand types are ‘Math::TVector<int, 3
error: no match for ‘operator-’ (operand types are ‘Math::TVector<int, 3ul>’ and ‘Math::TVector<int, 3ul>’)
错误:“运算符-”不匹配(操作数类型为“Math::TVector”和“Math::TVector”)
尽管它有一个功能:
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;
}
模板
数学::TVector运算符-(数学::TVector&第一,数学::TVector&第二)
{
数学:TVector结果;
for(无符号cont=0;cont
该代码的示例可在
我试着在名称空间内、名称空间外、名称空间外以完全解析(包括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>'
main.cpp:在函数“int main(int,char**)”中:
main.cpp:16:23:错误:“运算符-”不匹配(操作数类型为“Math::TVector”和“Math::TVector”)
向量1=向量1-向量2;
^
main.cpp:16:23:注:候选人为:
在main.cpp中包含的文件中:2:0:
点h:171:43:注意:模板数学::TVector运算符
-(数学::TVector&,数学::TVector&)
数学::TVector运算符-(数学::TVector&第一,数学::TVector&第二)
^
h点:171:43:注意:模板参数扣除/替换失败:
main.cpp:16:25:注意:dump#u type#不支持不匹配的类型“unsigned int”和“#”integer_cst”
向量1=向量1-向量2;
^
main.cpp:16:25:注意:“Math::TVector”不是从“Math::TVector”派生而来的
问题(或者至少是其中一个问题)似乎是您使用了unsigned
作为操作符-
的第二个非类型模板参数的类型,而类TVector
是用类型为std::size\u 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 )
{
// ...
}
模板
// ^^^^^^^^^^^
数学::TVector运算符-(
数学:TVector&第一,
数学:TVector和Second)
{
// ...
}
我怀疑错误输出中的行是否是唯一的一行,请编辑您的问题以包含完整且未编辑的错误输出。另外,请展示您用来调用函数的代码,包括所涉及变量的声明。请编辑这个问题。@Axalo那就更奇怪了。gcc不喜欢这段代码有什么错?@JoachimPileborg如果你遵循我在问题中提供的链接,你可以看到代码以及完整的错误消息非常好的主意,但它没有。。。还是同样的错误。@RhiakathFlanders:真奇怪。在对您链接的在线代码进行修改之后,我能够编译并执行。您链接的代码是否与您正在使用的代码相同?@RhiakathFlanders:另外,您是否确定您在同一函数调用中得到的错误与您在调用另一个尚未修复的函数时得到的错误完全相同?总结你的问题,明白了!由于它是一个标题,出于某种原因,cmake决定不重新编译它。我不得不做一个清洁/构建周期,现在一切都好了!这个尺寸是个问题。谢谢@RhiakathFlanders:很高兴这有帮助:)