Question:
How do I fix this: C++ error C2062 type 'double' unexpected?
c3
2010-09-11 15:29:23 UTC
I have to calculate the heat index in my program, with the user inputting the air temperature and relative humidity as int's, and the equation for heat index has a bunch of numbers that need to be put to a power of something. I have to use static_cast to convert the integers to doubles, and then use double pow( ) for the equation, I think? Here's the code for that section: (with airTemp, HEATINDEX, and relHumidity already having been declared earlier in the program)

//convert inputs from integers to doubles
static_cast (heatIndex);
static_cast (airTemp);
static_cast (relHumidity);

//calculate heat index
HEATINDEX = (-42.379) + (2.04901523 * airTemp) + (10.1433127 * relHumidity)
- (0.22475541 * airTemp * relHumidity)
- (6.83783 * pow(10.0, -3)) * double pow(airTemp, 2)
- ((5.481717 * pow(10.0, -2)) * double pow(relHumidity, 2))
+ ((1.22874 * pow(10.0, -3)) * pow(airTemp, 2) * relHumidity)
+ ((8.5282 * pow(10.0, -4)) * airTemp * double pow(relHumidity, 2))
- (1.99 * pow(10.0, -6)) * double pow(airTemp, 2) * double pow(relHumidity, 2);


When I try to compile it, it comes up with:
heatindex.cpp(74): error C2062: type 'double' unexpected
with line 74 being:
- (6.83783 * pow(10.0, -3)) * double pow(airTemp, 2)

How can I fix this?
Six answers:
The Phlebob
2010-09-11 16:10:33 UTC
The word "double" in that formula is confusing the compiler. It's not needed. The pow() function is either already double (don't remember) or will be cast to double because the rest of the calculation is already double. (Conversions from float to double are innocuous because all the bits are retained. No accuracy is lost. Conversions in the opposite direction, from double to float, need to be monitored.)



And those static_cast's at the top aren't doing anything. static_cast doesn't convert the referenced variable. It casts the value so it can be assigned to another variable of the cast type. In other words:



double d_heatIndex = static_cast (heatIndex);



C and C++ tend to let little historical syntax anomalies like this pass. If you set the compiler's error reporting level strict enough, even these might be flagged with warnings.



Hope that helps.
modulo_function
2010-09-11 17:45:45 UTC
Most of the answers that you've given a part right and part misleading.



Your original code was syntactically wrong. You wanted an explicit cast which you get with (double), the cast must be in parentheses. But, the pow(..) is overloaded so a float or double gives the same type back, thus the cast is not necessary.



I don't know why anyone is telling you to truncate. You should strive to know exactly what you want your numbers to do. What's with that warning C4244? Is that on the same line as before? If so, it implys that the left side of the assignment statement is an int. I cant' tell what the type of HEATINDEX is.



The link is to cplusplus.com, a great source. After looking at the different forms of pow(..) search for cast to see how it's done
coe
2016-10-25 04:58:58 UTC
What compiler/IDE are you making use of. If seen studio, then you incredibly must be able to double click the mistakes and this is going to educate which line it truly is. ok so that you're saying this is the first line? i think this is because you left of the semi-colon. it will be this: double height1, radians1, tangent1; except for, i do not see the variable "distanceToRainbow" declared everywhere. EDIT: After interpreting bill's answer, I in simple terms realized that you received't have the needed library. Do you've #contain iomanip on the right of your code someplace?
dick010453
2010-09-11 15:44:48 UTC
+ ((1.22874 * pow(10.0, -3)) * pow(airTemp, 2) * relHumidity)

I am NOT a C++ expert but I think the above line should be :-

+ ((1.22874 * pow(10.0, -3)) * double pow(airTemp, 2) * relHumidity)



but the way I would try to fix it is:- remove the additional "bits" of the formula until it compiles then add them in until it complains again ;-))



PS I know that you said it was the line before but ... compilers are funny things.
Ratchetr
2010-09-11 16:00:22 UTC
I think Dick got it right, except he reversed the 2 lines (what it is vs. what it should be).



Yeah....drop the world double out of there and I think you have it.
2010-09-11 16:15:44 UTC
"My teacher wants us to work the problem as floating arithmetic"



It is.



"and then convert back so you store an integer"



Truncate it and store it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...