【问题标题】:c++ template operator no match foundc ++模板运算符找不到匹配项
【发布时间】: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>' 

【问题讨论】:

  • 我怀疑错误输出中的那一行是唯一的,请编辑您的问题以包含 completeunedited 错误输出。还请显示您用于调用函数的代码,包括所涉及的变量的声明。
  • 编辑问题
  • @Axalo 然后就更奇怪了。 gcc 不喜欢该代码有什么问题?
  • @JoachimPileborg 如果您点击我在问题中提供的链接,您可以看到代码以及完整的错误消息

标签: c++ templates


【解决方案1】:

问题(或至少是其中一个问题)似乎是您使用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 )
{
    // ...
}

【讨论】:

  • 非常好的主意,但它没有......仍然是同样的错误。
  • @RhiakathFlanders:这很奇怪。在对您链接的在线代码进行修改后,我能够编译和执行。您链接的代码与您使用的代码相同吗?
  • @RhiakathFlanders:另外,你确定你得到的错误对于完全相同的函数调用是完全相同的,而不是对你尚未修复的另一个函数的调用? This example 总结了你的问题。
  • 知道了!由于它是一个头文件,出于某种原因,cmake 决定不重新编译它。我必须做一个清洁/构建周期,现在没关系!那个 size_t 是问题所在。谢谢!
  • @RhiakathFlanders:很高兴它有帮助:)
猜你喜欢
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
相关资源
最近更新 更多