While writing the following function abs, I get the error:
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;
}
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?
Your desire not to modify
tis expressed inconst T& t. The endingconstspecifies that you will not modify any member variable of the classabsbelongs to.Since there is no class where this function belongs to, you get an error.