无法将'double(_cdecl *)()'转换为'double'
作为一项任务,我必须编写一个接受用户输入的代码,并对它们执行操作,然后将它们打印到屏幕上。但是,我不断收到第18行的错误信息,其中我称FunctionMultiply为函数不能将'double(_cdecl *)()'转换为'double'。我搜索了这种类型的问题,但似乎所有这些都与我的代码中没有的数组有关。我怎样才能解决这个问题?无法将'double(_cdecl *)()'转换为'double'
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int GetInt(void);
double GetDouble();
char GetLetter(void);
double FunctionMultiply(int, double);
int FunctionCharacter(char);
int main()
{
GetInt();
GetDouble();
GetLetter();
FunctionMultiply(GetInt, GetDouble);
FunctionCharacter(GetLetter);
printf("%f", FunctionMultiply);
printf("%c", FunctionCharacter);
return 0;
}
int GetInt(void)
{
int integer;
printf("Enter an integer\n");
scanf("%i", &integer);
return integer;
}
double GetDouble()
{
double dub;
printf("Enter a floating point number\n");
scanf(" %lf", &dub);
return dub;
}
char GetLetter(void)
{
char letter;
printf("Enter a letter\n");
scanf(" %c", &letter);
return letter;
}
double FunctionMultiply(int arg1, double arg2)
{
double product = arg1 * arg2;
return product;
}
int FunctionCharacter(char letter)
{
if (toupper(letter) <= 'M')
{
return 0;
}
else
{
return 1;
}
}
看起来你并没有注意显示如何使用函数的课程或例子。
GetInt();
这调用GetInt
函数,并忽略它的返回值。
GetDouble();
这调用GetDouble
函数,并忽略它的返回值。
GetLetter();
这称为GetLetter
函数,并且...您现在知道得分。
FunctionMultiply(GetInt, GetDouble);
这只是无稽之谈。您正试图调用FunctionMultiply
函数,将函数GetInt
和GetDouble
作为参数传递。您需要通过它int
和double
,但您没有int
和double
,因为您没有将结果GetInt
和GetDouble
存储在任何地方。
你应该这样做:
int i = GetInt();
double d = GetDouble();
char l = GetLetter();
现在你有变量i
,d
和l
持有这些函数调用的结果,这样你就可以在其它功能通过他们:
FunctionCharacter(i, d);
在调用函数一次后,您似乎觉得函数的名称会奇迹般地改变为调用的结果。
它没有。函数调用表达式本身就是调用的结果。
而不是
ReturnADouble();
// call^and value v somehow separated? Why did you ever think that?
double result = ReturnADouble;
但根据语言规则,ReturnADouble
仍然是一个函数的名称,当你给一个函数的名称时,你应该给一个数值,编译器理直气壮地抱怨。
您的代码应该读起来更像
double result = ReturnADouble();
//^this call results in a value
我想你混淆了功能识别符与存储。你刚开始时调用了一堆函数,并没有将结果存储在任何东西中。
看来你期望自己使用函数标识符会给你最后一次调用该函数的结果。但事实并非如此。
这里是你如何将存储返回值,并在以后使用它们:
int my_int = GetInt();
double my_double = GetDouble();
char my_char = GetLetter();
double multiply_result = FunctionMultiply(my_int, my_double);
char char_result = FunctionCharacter(my_char);
printf("%f", multiply_result);
printf("%c", char_result);
修改您的main()
这样的:
int main()
{
int i = GetInt();
double d = GetDouble();
char c = GetLetter();
double a = FunctionMultiply(i, d);
char c = FunctionCharacter(c);
printf("%f", a);
printf("%c", c);
return 0;
}
你的问题是,你正在传递函数名称,而不是调用他们。即GetInt
而不是GetInt()
。
FunctionCharacter(GetLetter()); –
[mcve]会更容易 – anatolyg