非成员函数不能具有cv限定词 [英] non-member function cannot have cv-qualifier
问题描述
在编写以下函数 abs
时,出现错误:
While writing the following function abs
, I get the error:
非成员函数 unsigned int abs(const T&)
不能具有简历限定符。
non-member function unsigned int abs(const T&)
cannot have cv-qualifier.
template<typename T>
inline unsigned int abs(const T& t) const
{
return t>0?t:-t;
}
删除 const
该函数的限定符没有错误。由于我没有在函数内修改 t
,因此上述代码应该已经编译。我想知道为什么会收到错误消息?
After removing the const
qualifier for the function there is no error. Since I am not modifying t
inside the function the above code should have compiled. I am wondering why I got the error?
推荐答案
您希望不修改 t
用 const T& t
。 const
结尾表示您将不会修改 abs
所属的类的任何成员变量。
Your desire not to modify t
is expressed in const T& t
. The ending const
specifies that you will not modify any member variable of the class abs
belongs to.
由于该函数所属的类都不存在,因此会出现错误。
Since there is no class where this function belongs to, you get an error.
这篇关于非成员函数不能具有cv限定词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!