简体   繁体   中英

C++ Functions - Error: '0' cannot be used as a function

I'm new to functions and trying to understand what I've done wrong. My build message spits out the error: '0' cannot be used as a function and highlights the line return ((5 / 9)(fahrenheit - 32)); within the function. Thanks in advance for any advice.

#include <iostream>
using namespace std;

double celsiusFunction(double fahrenheit);

int main()
{
    double fahrenheitTemp;

    fahrenheitTemp = celsiusFunction(99);
    cout << fahrenheitTemp;

    return 0;
}

double celsiusFunction(double fahrenheit)
{
    return ((5 / 9)(fahrenheit - 32));
}
  1. 5 / 9 is 0 , because both are integers and thus it's evaluated in integer arithmetic. Do this instead: 5.0 / 9.0 to get floating results.

  2. You're not multiplying in the return statement, so the compiler interprets the second parentheses as a funciton call (that is, calling 5 / 9 with arguments fahrenheit - 32 ). This is of course nonsense. Do this:

     return (5.0 / 9.0) * (fahrenheit - 32.0); 
((5 / 9)(fahrenheit - 32))
 \_____/\_______________/
    1           2

2 is interpreted as a function call on 1. You forgot the multiplication:

((5 / 9) * (fahrenheit - 32))

你忘记了*操作符

You should change return ((5 / 9)(fahrenheit - 32)); to

return ((5 / 9)*(fahrenheit - 32));

Add * after (5/9) . Because of missing * you are getting the error.

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