I’m trying to overload the operator % because you can’t use modulus on double types,
float a = 5.0;
float b = 5.0;
a = a % b;
// not allowed
I Was trying to overload the operator % with this kind of function :
template <>
MyClass* MyClass<float>::operator%(Myclass &other)
For other operation non involving float I use :
template <class T>
MyClass* MyClass<T>::operator%(MyClass &other)
It never compiled actually I’m stuck and can’t find a way to bypass this problem,
g++ is still warning me that you can’t perform modulo on floats, is something wrong
with my template syntax or is it really impossible.
You can’t overload operators for primitive types the way you’d want it to work.
For C++11 draft n3290, §13.5 Operator Overloads, point 6:
Primitive types aren’t classes (or enums), so they can’t have member functions. And you can’t create a global
float operator%(float&,float&)since that doesn’t involve a class or enum in the parameter list. (See also C++FAQ 26.10 “Can I define an operator overload that works with built-in / intrinsic / primitive types?”.)You need at least one of the terms in the
%expression to be a user-defined type.You could create a class
Floatand define whatever operations you want on it, but you cannot geta = a % b;to use your function if bothaandbarefloats.Or you could
#include <cmath>and usestd::fmod:Simple example with a custom “float wrapper” (incomplete, probably not quite safe as-is, but can get you started):