简体   繁体   中英

Using atan2 with a class template fails with ambigous call to overloaded function

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 atan2 depending on the types of its input arguments. If a and b in your code snippet are different types, then overload resolution will fail since the call is ambiguous.

You need to cast a or b as appropriate so that their types match.

If you intended to call atan2(double, double) , an alternate solution would be to include math.h instead of cmath and then call the function as ::atan2( a, b ) . This will implicitly convert both a and b to double (if such a conversion is possible).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM