我得到的错误是“表达式必须在c ++中具有(指向 - 指向)函数类型”

时间:2014-01-04 14:23:20

标签: c++ visual-studio-2010

您好我有以下代码

# include <iostream>
# include <limits>
# include <cmath>

using namespace std;

class fahrenheit
{
    float f,c,x;
public:
    void getdata();
    void display();
}

void fahrenheit::getdata()
{
    cout << "Enter the value of f : ";
    cin >> f;
    x=f-32;
    c=5/9(x);   //Here i am getting error as Expression must have (pointer-to-)function type //
}

void fahrenheit::display()
{
    cout << "c=" << c;
    std::cin.ignore();
    std::cin.get();

}

int main()
{
    fahrenheit f;
    f.getdata();
    f.display();
}

我已经为输入变量指定了数据类型为float,但我不确定应该采取什么措施来纠正错误。

3 个答案:

答案 0 :(得分:3)

5/9(x)远不像C ++。你可能意味着c = 5.0 / 9.0 * x;

答案 1 :(得分:1)

首先,你在课程定义之后忘记了分号。 其次,我认为你想把x乘以9.写         C = 5/9 *(x)的

否则编译器会尝试找到一个名为9(int x)的函数(无论如何都是函数的错误名称)并且意识到9在任何意义上都不是任何函数指针而只是int ..这是这个错误意味着什么。

顺便说一句..如果你写5/9编译器将其理解为被分割的int值。 它将使用int /运算符将int(5)除以9,之后将返回 floor(5/9) = 0。如果你想要floatdouble除法,你必须通知编译器你的值是浮点数(双精度数)。

对于双打:5.0/9.0*x 对于花车:5.0f/9.0f * x

答案 2 :(得分:0)

您应该使用乘法运算符*

c=5/9*(x);