C++ 错误:无法转换‘;浮动(*)和#x2019;至‘;浮动’;

C++ 错误:无法转换‘;浮动(*)和#x2019;至‘;浮动’;,c++,C++,我的程序将温度从华氏标度转换为摄氏标度,最后转换为绝对值标度 #include <iostream> #include <cmath> #include <iomanip> using namespace std; int farh; float cels(int a) { float c; const int m0 = 32; const float m1 = 0.5555; c=(a-m0)/m1; retur

我的程序将温度从华氏标度转换为摄氏标度,最后转换为绝对值标度

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int farh;

float cels(int a)
{
    float c;
    const int m0 = 32;
    const float m1 = 0.5555;

    c=(a-m0)/m1;
    return c;
}

float ab(float a)
{
    const float m2 = 273.15;
    float d;

    d=a-m2;
    return d;
}

int main() {
    const int WIDTH = 16;

    cout << setiosflags ( ios :: left );
    cout << setw(WIDTH) << "Fahrenheit" << setw(WIDTH) << "Celcius" << setw(WIDTH) << "Absolute Value" << '\n';

    cout.setf(ios::fixed);
    cout.precision(2);

    for (farh = 0 ; farh <= 300 ; farh = farh + 20) {
        cout.width(16);
        cout << farh << cels(farh) << ab(cels) << "\n";
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
因特法赫;
浮点数(int a)
{
浮点数c;
常数int m0=32;
常数浮点m1=0.5555;
c=(a-m0)/m1;
返回c;
}
浮动ab(浮动a)
{
常数浮动m2=273.15;
浮动d;
d=a-m2;
返回d;
}
int main(){
const int WIDTH=16;

cout
ab
接受一个
float
并返回一个
float

float ab(float a)
但是
cels
不是一个
float
,它是一个函数:

float cels(int a)
你可能是说

ab(cels(farh))
或采取临时措施:

float cur_cels = cels(farh);
cout << farh << cur_cels << ab(cur_cels) << "\n";
float cur_cels=cels(farh);

cout实际上,您已经向ab传递了一个函数指针。如果您的目的是传递函数(显然不是!),则可以使用以下语法:

float ab(float(*callback)(int),int pass) {
  callback(pass); /* It calls the function indirectly with <pass> */
}
float ab(float(*回调)(int),int pass){
回调(pass);/*它使用*/
}
它非常适合创建菜单,例如,如果您有两个选项: 1.至电池的华氏温度 2.华氏度到开尔文,这将是有用的

你可以用谷歌搜索C中的回调函数。

@BAarry与ab(cels(farh))完美结合。
float ab(float(*callback)(int),int pass) {
  callback(pass); /* It calls the function indirectly with <pass> */
}