工具/软件:TI C/C++编译器
我将在我们的代码库上验证18.1.4.LTS、并得到错误诊断:
>>编译失败 ServoLibrary/MotionControl/Brak/subdir_rules.mk:9:目标'ervoLibrary/MotionControl/Brak/Brakil.obj 的配方失败 "C:\devel\ServoApplication\ServoLibrary\hal\TMS320C28346\include\.\.\.\.\.\.\.\.\.\.\service\math\math\absolute.h:1574:#math:#mutterment-ral:-math operator+(const 服务::数学:相对 &、const 服务::数学:绝对 &)"声明一个非模板函数-添加<>以引用模板实例 "C:\devel\ServoApplication\ServoLibrary\hal\TMS320C28346\include\..\..\..\..\service\math\absolute.h"、第90行:错误#1581-D:"service::::::math: absolute operator-(const 服务::数学::相对 &、const 服务::数学:绝对 &)"声明非模板函数-添加<>以引用模板实例 "C:\devel\ServoApplication\ServoLibrary\hal\TMS320C28346\include\..\..\..\..\service\math\relative .h"、第130行:错误#1581-D:"service:::::math:absolute operator+(const 服务::数学::绝对 &、const 服务::数学:相对 &)"声明非模板函数-添加<>以引用模板实例 "C:\devel\ServoApplication\ServoLibrary\hal\TMS320C28346\include\..\..\..\..\service\math\relative .h"、第160行:错误#1581-D:"service:::::math:absolute operator-(const 服务::数学::绝对 &、const 服务::数学:相对 &)"声明非模板函数--添加<>以引用 在编译中检测到的模板实例4错误...
显然、编译器认为模板类的非内联好友必须是模板本身。 但是,在这种情况下,朋友是在伙伴类中内联定义的,并且存在朋友声明,以便在必须从兼容类型隐式构建两个参数之一的情况下启用参数相关查找。
以下是相关代码减去注释(以使其更紧凑):
#ifndef _service_Math_Absolute_H_ #define _service_Math_Absolute_H_ 命名空间服务{ 命名空间数学{ template 结构相对; 模板 struct absolute { 绝对值(t const& rvalue):mValue (rvalue){} 朋友绝对是 运算符+(绝对值 CONST& rLSummand、 相对 CONST& rRMAND) { 返回 rLSummand.mValue + rRSummand.mValue; } //错误#1581 朋友绝对是 运算符+(相对 const&、绝对 const&); 朋友绝对是 运算符-(绝对值 const& rMinuend、 相对 const& rSubtrahend) { 返回 rMinuend.mValue - rSubtrahend.mValue; } //错误#1581 朋友绝对是 运算符-(相对 const&、绝对 const&); 亲戚朋友 运算符-(绝对值 const& rMinuend、 绝对值 const& rSubtrahend) { 返回 rMinuend.mValue - rSubtrahend.mValue; } Friend bool 运算符==(绝对值 const& rLs、绝对值 常量和右量) { 返回 rLhs.mValue = rRhs.mValue; } Friend bool 运算符!=(绝对值 const& rLs、绝对值 常量和右量) { 返回 rLhs.mValue!= rRhs.mValue; } Friend bool 运算符<(绝对值 const& rLs、绝对值 常量和右量) { 返回 rLhs.mValue < rRhs.mValue; } Friend bool 运算符<=(绝对值 const& rLs、绝对值 常量和右量) { 返回 rLhs.mValue <= rRhs.mValue; } 朋友 bool 运算符>(绝对 const& rLs、绝对值 常量和右量) { 返回 rLhs.mValue > rRhs.mValue; } Friend bool 运算符>=(绝对值 const& rLs、绝对值 常量和右量) { 返回 rLhs.mValue >= rRhs.mValue; } t mValue; }; }/*命名空间数学*/ }/*命名空间服务*/ #endif /*_service_Math_Absolute_H_*/
#ifndef _service_Math_Relate_H_ #define _service_Math_Relate_H_ 命名空间服务{ 命名空间数学{ template struct absolute; template 结构相对; 模板 直列相对 操作员%(相对 const& rDivent、 相对 const& rQuotient) { 返回 rDivident.mValue % rQuotient.mValue; } 模板 结构相对 { Relative (T const& rvalue):mValue (rvalue){} 亲戚朋友 运算符+(相对 CONST& ROP) { 返回 ROP; } 亲戚朋友 运算符-(相对 CONST& ROP) { 返回- ROP.mValue; } //错误#1581
朋友绝对是 运算符+(绝对值 const&、相对 const&); 朋友绝对是 运算符+(相对 CONST& rLSummand、 绝对值 CONST& rRMAND) { 返回 rLSummand.mValue + rRSummand.mValue; } 亲戚朋友 运算符+(相对 CONST& rLSummand、 相对 CONST& rRMAND) { 返回 rLSummand.mValue + rRSummand.mValue; } //错误#1581
朋友绝对是 运算符-(绝对值 const&、相对 const&); 朋友绝对是 运算符-(相对 const& rMinuend、 绝对值 const& rSubtrahend) { 返回 rMinuend.mValue - rSubtrahend.mValue; } 亲戚朋友 运算符-(相对 const& rMinuend、 相对 const& rSubtrahend) { 返回 rMinuend.mValue - rSubtrahend.mValue; } 亲戚朋友 运算符*(相对 const&rLFactor、 相对 常量和 rRFactor) { 返回 rLFactor.mValue * rRFactor.mValue; } 亲戚朋友 运算符/(相对 const& rDivent、 相对 const& rQuotient) { 返回 rDivident.mValue /rQuotient.mValue; } 亲戚朋友 操作员%(相对 const& rDivent、 相对 const& rDivisor) { 退货操作员% (rDivident、rDivisor); } 朋友 bool 运算符==(Relative 常量和左侧、相对 常量和右量) { 返回 rLhs.mValue = rRhs.mValue; } 朋友 bool 运算符!=(相对 常量和左侧、相对 常量和右量) { 返回 rLhs.mValue!= rRhs.mValue; } 朋友 bool 运算符<(相对 常量和左侧、相对 常量和右量) { 返回 rLhs.mValue < rRhs.mValue; } Friend bool 运算符<=(相对 常量和左侧、相对 常量和右量) { 返回 rLhs.mValue <= rRhs.mValue; } 朋友 bool 运算符>(相对 常量和左侧、相对 常量和右量) { 返回 rLhs.mValue > rRhs.mValue; } Friend bool 运算符>=(相对 常量和左侧、相对 常量和右量) { 返回 rLhs.mValue >= rRhs.mValue; } t mValue; }; #pragma diag_pop }/*命名空间数学*/ }/*命名空间服务* /#endif /*_service_Math_Relative _H_*/