So I'm writing a bit of code that needs to raise a function's return value to a certain power. I recently discovered that using the '^' operator for exponentiation is useless because in C++ it is actually an XOR operator or something like that. Now here's the code I want to write:
int answer = pow(base, raisingTo(power));
Now can anyone tell me if this is right? I'll explain the code. I've declared an int variable answer as you all are aware of, and initialized it to the value of any variable called 'base', raised to the return value of the raisingTo() function acting on any other variable called 'power'. When I do this (and I edit & compile my code in Visual C++ 2010 Express Edition), a red dash appears under the word 'pow' and an error appears saying: "more than one instance of overloaded function 'pow' matches the argument list"Can someone please solve this problem for me? And could you guys also explain to me how this whole pow() function actually works, cos frankly www.cplusplus.com references are a little confusing as I am still only a beginner!
The documentation states it pretty explicitly already:
The pow(int, int) overload is no longer available. If you use this overload, the compiler may emit C2668 [EDIT: That's the error you get]. To avoid this problem, cast the first parameter to double, float, or long double.
Also, to calculate basepower you just write
pow(base, power)
And with above hint:
int result = (int) pow((double)base, power);
The pow() function returns either a double or a float, so the first step would be to change answer to one of those. Second, what is raisingTo() returning. Unless your are doing something that's not evident, you don't need that, but it should still work. Also, both arguments should be doubles, according to this.
Related
I am attempting to convert a tool from C to C++ so I can compile as a CLR. I am a .NET guy so this is well outside my comfort zone.
I have a compile error on the following line (tell me if this is not enough information):
if (qvartype[ currQ ] == FLOATING )
*range *= get_scale( currQ ); /* Make range units match */
/* data units. */
currQ is a short. The error is defined on the get_scale function. This function is defined earlier as:
#define get_scale( i ) ((short)pow( 10, (int)((long)(cat_names[ i ]))))
...which looks ridiculous to me, deep into type casting hell, but it does compile in C. However, in C++, I get the following error message:
Error 374 error C2668: 'pow' : ambiguous call to overloaded function
I understand C does not employ the concept of overloads, but C++ does, and the variable signature on this hot mess makes it unclear which function to invoke.
How do I fix this? What would be the right way to write this for maximum compatibility with C++?
There is no overloaded version of pow() in C++ which satisfies your calling signature of (int, int). One of the supported calling conventions is (double, int) so modifying your call to:
pow(10.0, ...)
should be enough
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 that is using a macro, and i'm not too comfortable with those. 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/
According to
https://msdn.microsoft.com/en-us/library/dt5dakze.aspx
there are many pow() variants. You're calling pow() with type casting, but the compiler has few functions matching and gets confused.
Change the definition of get_scale() to be more specific about types, so the compiler can select proper pow() version unambiguously.
i have used srand(time(0)) in my program to generate random numbers ,the problem is my compiler shows an error that call of overloaded 'srand(time_t)' is ambiguous.i don't know what have i done wrong.
please help. i wanted to generate random numbers to be used in my minesweeper program , but it is giving this error. normally in any other program where i am simply outputting the value it works okay , but in this case i am initializing the random value to a variable. i don't know what to do
It means you have a non-standard overload of srand declared somewhere for some reason.
Either seek and destroy the rogue overload; or specify std::srand (if your standard library implementation isn't at fault); or convert the argument type to unsigned int to unambiguously choose the standard one.
Also, have a look at the new(ish) C++ random number library: http://en.cppreference.com/w/cpp/numeric/random. It's a bit more complicated, but much more flexible, and without the evil of global state.
I am using a code quality tool called Parasoft C++ Test. It is complaining about the following:
setsockopt(...,sizeof(int));
It states:
In 'setsockopt' function call, do not pass long casted to int expression as '5' function argument
I have tried several different casting options to resolve this, but none of them seem to have any effect. I can do the following and make it go away:
socklen_t socklength = sizeof(int);
setsockopt(...,socklength);
Is there a way to call sizeof(int) inline with the setsockopt call without generating this quality error?
Cast to a socklen_t? something like: static_cast<socklen_t>(sizeof(int)), or you can just ignore it...
In general, the answer is to ignore this warning - it's clearly bogus. Further, the fact that you are having to spend time trying to fix a warning generated against a system header file indicates that something is amiss.
You might try casting the sizeof() to socklen_t, but I expect you already have. The other somewhat obvious thing would be to construct an expression of type int. Perhaps something like:
sizeof( int ) & 0xFF.
(Of course, on systems where integers are more than 2^255 bits wide, this might fail... :)
Pretty simple question I think but I'm having trouble finding any discussion on it at all anywhere on the web. I've seen the triple-dot's as function parameters multiple times throughout the years and I've always just thought it meant "and whatever you would stick here." Until last night, when I decided to try to compile a function with them. To my surprise, it compiled without warnings or errors on MSVC2010. Or at least, it appeared to. I'm not really sure, so I figured I'd ask here.
They are va_args, or variable number of arguments. See for example The C book
Triple dots means the function is variadic (i.e. accepts a variable number of parameters). However to be used there should be at least a parameter... so having just "..." isn't an usable portable declaration.
Sometimes variadic function declarations are used in C++ template trickery just because of the resolution precedence of overloads (i.e. those functions are declared just to make a certain template instantiation to fail or succeed, the variadic function themselves are not implemented). This technique is named Substitution failure is not an error (SFINAE).
It's called ellipses - basically saying that function accepts any number of arguments of any non-class type.
It means that the types of arguments, and the number of them are unspecified. A concrete example with which you are probably familiar would be something like printf(char *, ...)
If you use printf, you can put whatever you like after the format string, and it is not enforced by the compiler.
e.g. printf("%s:%s",8), gets through the compiler just the same as if the "expected" arguments are provided printf("%s:%s", "stringA", "stringB").
Unless really necessary, it should be avoided, as it creates the potential for a run time error to occur, where it might otherwise have been picked up at compile time. If there is a finite, enumerable variation in the arguments your function can accept, then it is better to enumerate them by overloading.
When I moved a program from a Mac to this Windows PC, the VC++ 2008 compiler is giving me errors for passing unsigned ints to the cmath pow() function. As I understand, this function is not overloaded to accept anything but floating-point numbers.
Is there some compiler flag/setting that will ignore these errors? Also does anyone know how to find the documentation on the VC++ compiler?
Edit
This isn't a warning, it's an error. However, for me it's not an issue since my program is only dealing with numbers that come out as integers, so I don't care that they aren't floats. If it was just warnings I would move on with my life, but it's not letting me compile. Can I suppress errors somehow? Like I said, the errors aren't coming up on my Mac and the program is fine.
Regarding other answers here, it is not a good idea to tell the question author to turn off this warning. His code is broken - he's passing an unsigned int instead of a float. You should be telling him to fix his code!
This isn't a warning, it's an error. However, for me it's not an issue since my
program is only dealing with numbers that come out as integers, so I don't care that
they aren't floats. If it was just warnings I would move on with my life, but it's not
letting me compile. Can I suppress errors somehow? Like I said, the errors aren't
coming up on my Mac and the program is fine.
Integers and floats use different representations internally. If you have the same number in an int and a float, the bit pattern inside the storage for them is completely different. You cannot under any circumstances whatsoever expect your code to work if you are passing an integer when you should be passing a float.
Furthermore, I assert your Mac code either is silently using an overloaded version of that function (e.g. you are on that platform compiling with C++) or you believe it works when in fact it is working by chance or is not actually working.
Addendum
No compilers ever written has the ability to turn off errors.
A warning means the compiler thinks you're making a mistake.
An error means the compiler doesn't know what to do.
There are a couple of options:
In C, the solution is simply to cast the ints to doubles:
pow((double)i, (double)j)
In C++, you can do the same, although you should use a C++-style cast:
pow(static_cast<double>(i), static_cast<double>(j))
But a better idea is to use the overload C++ provides:
std::pow(static_cast<double>(i), j);
The base still has to be a floating-point value, but the exponent can be an int at least
The std:: prefix probably isn't necessary (most compilers make the function available in the global namespace as well).
Of course, to access the C++ versions of the function, you have to include the C++ version of the header.
So instead of #include <math.h> you need to #include <cmath>
C++ provides C++ versions of every C header, using this naming convention. If the C header is called foo.h, the C++ version will be cfoo. When you're writing in C++, you should always prefer these versions.
I don't know of a flag, but getting rid of the warnings was easy enough for me. Just double click on each of the warnings in the "Task List" and add the appropriate casting, whether you prefer
(double) my_variable
or
static_cast<double>(my_variable)
I'm guessing if you're getting the ambiguous warning, there are multiple pow functions defined somewhere. It's better to be explicit in my opinion anyway. For what it's worth, my vote goes with the static_cast option.
As Mehrdad mentioned, use the #pragma warning syntax to disable a warning. Documentation is here - http://msdn.microsoft.com/en-us/library/2c8f766e.aspx
I would be inclined to fix the warnings rather than hide them though!
C++ has overloads for pow/powf for int exponents. Heed the warning.
Don't ignore this or any warnings. Fix them. The compiler is your friend, trying to get you to write good code. It's a friend that believes in tough love, but it is your friend.
If you have an unsigned int and need a float, convert your unsigned in to a float.
And the MSDN Library is the documentation for both the VC++ implementation of the language and the IDE itself.