简体   繁体   English

在c ++中出现“表达式必须具有(指针到)函数类型”的错误

[英]Am getting error as “Expression must have (pointer-to-)function type” in c++

Hi i have the below code 嗨,我有以下代码

# 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();
}

i have given the datatype as float for the input variables , but i am not sure what should be done to rectify the error . 我已经将数据类型指定为输入变量的float类型,但是我不确定应该采取什么措施来纠正错误。

5/9(x) doesn't remotely look like C++. 5/9(x)看起来并不像C ++。 You likely meant c = 5.0 / 9.0 * x; 您可能意味着c = 5.0 / 9.0 * x;

first of all, you forgot about a semicolon right after the class definition. 首先,您在类定义之后就忘记了分号。 Second, I presume you wanted to multiply x by 9. Write c=5/9*(x) 第二,我假设您想将x乘以9。写c = 5/9 *(x)

otherwise the compiler tries to find a function called 9(int x) (which is an incorrect name for a function anyway) and realizes that 9 is in no sense any function pointer but just an int .. that's what the error means. 否则,编译器将尝试查找一个称为9(int x)的函数(无论如何该函数的名称都不正确),并意识到9在任何意义上都不是任何函数指针,而只是一个int ..这就是错误的含义。

By the way.. if you write 5/9 compiler understands it as int values being divided. 顺便说一句..如果您编写5/9,编译器会将其理解为被除以int值的形式。 It will divide int(5) by 9 using an int / operator, which after dividing will return floor(5/9) = 0 . 它将使用int /运算符将int(5)除以9,除法后将返回floor(5/9) = 0 If you want to have a float or double division you have to inform the compiler that your values are floats(doubles). 如果要进行floatdouble除,则必须通知编译器您的值是浮点(双精度)。

For doubles: 5.0/9.0*x For floats: 5.0f/9.0f * x 双打: 5.0/9.0*x 5.0f/9.0f * x5.0f/9.0f * x

您应该使用乘法运算符*

c=5/9*(x);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表达式必须具有(指向)函数类型 - expression must have (pointer-to-) function type 错误:表达式必须具有(指针指向)函数类型 - Error: Expression must have (Pointer-to-) function type 使用结构时,表达式必须具有(指针指向)函数类型 - Expression must have (Pointer-to-) function type while using structs 在模板类函数定义中使用“ this”指针—表达式必须具有(pointer-to-)函数类型 - Using “this” pointer in template class function definition — expression must have (pointer-to-) function type 使用MAKEWORD函数会创建错误E0109-在显式调用括号之前的表达式必须具有(pointer-to-)函数类型 - using MAKEWORD function creates error E0109- expression preceding parentheses of apparent call must have (pointer-to-) function type 从dll进行的函数调用[明显调用的括号前的表达式必须具有(pointer-to-)函数类型] - function call from dll [expression preceding parentheses of apparent call must have (pointer-to-) function type] 使用函子时,括号前的表达式必须具有指向函数的指针 - Expression preceding parentheses must have pointer-to- function type when using functors 明显调用括号前的表达式必须具有(指向)函数类型 - expression preceding parentheses of apparent call must have (pointer-to-) function type 为什么我在 c++ 中得到表达式必须有指向 class 类型的指针? - Why am I getting expression must have pointer to class type in c++? C ++类向量错误,表达式必须具有指针类型 - C++ Class vector error, expression must have pointer type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM