[英]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). 如果要进行
float
或double
除,则必须通知编译器您的值是浮点(双精度)。
For doubles: 5.0/9.0*x
For floats: 5.0f/9.0f * x
双打:
5.0/9.0*x
5.0f/9.0f * x
: 5.0f/9.0f * x
您应该使用乘法运算符*
。
c=5/9*(x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.