I’m trying to use the function atan2 in my class template but it’s not working. I’ve got a class called myclass and I’m trying make a template of the functions, this function is to take the tan of two numbers, a and b. These could either both be int or both be doubles
template <class T>
T myclass<T>::returnArg()
{
T arg(0);
arg = atan2(a, b);
return arg;
}
But I get error C2668: 'atan2' : ambiguous call to overloaded function. Can anyone suggest something to fix this?
Thank you.
Edit: I would like to be able to pass ints and doubles to the atan2 function, I have tried
arg = atan2(<T> a, <T> b);
But that didn’t work.
Edit 2: I declare a and b in my class as
template <class T> class myclass
{
private:
T a,b;
public:
myclass(): a(0),b(0){};
myclass(T r, T i) : a(r), b(i){};
// ...
C++ defines several overloads for
atan2depending on the types of its input arguments. Ifaandbin your code snippet are different types, then overload resolution will fail since the call is ambiguous.You need to cast
aorbas appropriate so that their types match.If you intended to call
atan2(double, double), an alternate solution would be to includemath.hinstead ofcmathand then call the function as::atan2( a, b ). This will implicitly convert bothaandbtodouble(if such a conversion is possible).