C to C ++, lost in translation

I am trying to convert a tool from C to C ++, so I can compile it as a CLR. I am a .NET guy, so this is far beyond my comfort zone.

I have a compilation error in the following line (tell me if there is not enough information):

if (qvartype[ currQ ] == FLOATING ) *range *= get_scale( currQ ); /* Make range units match */ /* data units. */ 

currQ is short . The error is defined in the get_scale function. This function is defined earlier:

 #define get_scale( i ) ((short)pow( 10, (int)((long)(cat_names[ i ])))) 

... which looks ridiculous to me, deep in a hellish style, but it compiles in C. However, in C ++, I get the following error message:

 Error 374 error C2668: 'pow' : ambiguous call to overloaded function 

I understand that C does not use the concept of overloads, but C ++ does, and the signature of the variable in this hot mess does not make it clear which function to call.

How to fix it? What would be the right way to write this for maximum compatibility with C ++?

+5
source share
3 answers

In C ++, there is no overloaded version of pow () that satisfies your call signature (int, int) . One of the supported calling conventions is (double, int) , so you can change your call to:

 pow(10.0, ...) 

should be enough

+4
source

in C ++, the pow function has the following signature

 double pow (double base, double exponent); 

so if you made you call something like

 #define get_scale( i ) ((short)pow( 10.0, (double)cat_names[ i ]))) 

but it’s using a macro, and I'm not too comfortable with it. I would create a function

 short get_scale(int i){return (short)pow( 10.0, (double)cat_names[ i ])}; 

see http://www.cplusplus.com/reference/cmath/pow/

0
source

According to

https://msdn.microsoft.com/en-us/library/dt5dakze.aspx

There are many variations of pow() . You call pow() with a casting type, but the compiler has several functions that match and get confused.

Change the definition of get_scale() more specifically about types, so the compiler can choose the correct version of pow() uniquely.

0
source

Source: https://habr.com/ru/post/1214611/


All Articles