[英]Inside Function getting recognized as a Pointer
When I try to compile, I get two error messages, which I thought about for a while and could not resolve. 当我尝试编译时,我收到两条错误消息,我想了一会儿并且无法解决。 Could somebody shed some light on my problem? 有人可以阐明我的问题吗?
errors: 错误:
leeftijd.cc: In function ‘int main()’:
leeftijd.cc:50:49: error: invalid conversion from ‘int’ to ‘int (*)(int, int, int)’ [-fpermissive]
leeftijd.cc:10:9: error: initializing argument 1 of ‘int leeftijd_in_maanden(int (*)(int, int, int), int, int, int)’ [-fpermissive]
Code: 码:
#include <iostream> // Line one
//line two
#define PEILJAAR 2012
#define PEILMAAND 9
#define PEILDAG 23
using namespace std;
int leeftijd_in_jaren(int _geboortejaar, int _geboortemaand, int _geboortedag);
int leeftijd_in_maanden(int _leeftijd_in_jaren(int _geboortejaar, int _geboortemaand, int _geboortedag),
int _geboortejaar, int _geboortemaand, int _geboortedag);
bool onjuiste_geboortedag(int _geboortedag, int _geboortejaar);
bool onjuiste_geboortemaand(int _geboortemaand, int _geboortejaar);
bool onjuiste_geboortejaar(int _geboortejaar);
int main()
{
int geboortejaar, geboortemaand, geboortedag;
cout << " Geef uw geboortejaar" << endl;
cin >> geboortejaar;
if( onjuiste_geboortejaar(geboortejaar) )
{
cout << "U voldoet niet aan de eisen." << endl;
return 0;
}
cout << " Geef uw geboortemaand" << endl;
cin >> geboortemaand;
if( onjuiste_geboortemaand(geboortemaand, geboortejaar) )
{
cout << "U voldoet niet aan de eisen." << endl;
return 0;
}
cout << " Geef uw geboortedag" << endl;
cin >> geboortedag;
if( onjuiste_geboortedag(geboortedag, geboortejaar) )
{
cout << "U voldoet niet aan de eisen." << endl;
return 0;
}
cout << "U bent "
<< leeftijd_in_jaren(geboortejaar, geboortemaand, geboortedag)
<< " jaar en "
<< " maanden en uw leeftijd in maanden is "
<< leeftijd_in_maanden( leeftijd_in_jaren(geboortejaar, geboortemaand, geboortedag),
geboortejaar, geboortemaand, geboortedag)
<< "." << endl;
return 0;
}
int leeftijd_in_jaren(int _geboortejaar, int _geboortemaand, int _geboortedag)
{
int _leeftijd_in_jaren;
if(_geboortemaand < PEILMAAND || _geboortemaand == PEILMAAND && _geboortedag <= PEILDAG)
{_leeftijd_in_jaren = PEILJAAR - _geboortejaar;}
else{_leeftijd_in_jaren = PEILJAAR - _geboortejaar - 1;}
return _leeftijd_in_jaren;
}
int leeftijd_in_maanden(int _leeftijd_in_jaren(int _geboortejaar, int _geboortemaand, int _geboortedag),
int _geboortejaar, int _geboortemaand, int _geboortedag)
{
int _leeftijd_in_maanden =
_leeftijd_in_jaren(_geboortejaar, _geboortemaand, _geboortedag) * 12 + _geboortemaand;
return _leeftijd_in_maanden;
}
bool onjuiste_geboortejaar(int _geboortejaar)
{return((PEILJAAR - _geboortejaar) < 10 || (PEILJAAR - _geboortejaar) > 100);}
bool onjuiste_geboortemaand(int _geboortemaand, int _geboortejaar)
{
return( (PEILJAAR - _geboortejaar) == 10 && _geboortemaand > PEILMAAND ||
(PEILJAAR - _geboortejaar) == 100 && _geboortemaand > PEILMAAND ||
_geboortemaand < 0 || _geboortemaand > 12);
}
bool onjuiste_geboortedag(int _geboortedag, int _geboortejaar)
{
return( PEILDAG <= _geboortedag && (PEILJAAR - _geboortejaar) == 10 ||
PEILDAG >= _geboortedag && (PEILJAAR - _geboortejaar) == 100||
_geboortedag < 0 || _geboortedag > 31);
}
int leeftijd_in_maanden(int _leeftijd_in_jaren(int _geboortejaar, int _geboortemaand, int _geboortedag),int _geboortejaar, int _geboortemaand, int _geboortedag)
The above is invalid function pointer declaration in parameter lists. 上面是参数列表中无效的函数指针声明。
Use:- 采用:-
int leeftijd_in_maanden(int (*funcpoint)(int , int , int ),int _geboortejaar, int _geboortemaand, int _geboortedag)
{
int _leeftijd_in_maanden =
funcpoint(_geboortejaar, _geboortemaand, _geboortedag) * 12 + _geboortemaand;
return _leeftijd_in_maanden;
}
In fact a function declaration in a parameter list is perfectly valid. 实际上,参数列表中的函数声明是完全有效的。 The rule is similar to the rule for an array parameter declaration; 该规则类似于数组参数声明的规则。 the type is adjusted from "function ..." to "pointer to function ...". 类型从“功能...”调整为“功能指针”。
For example, in the following the function foo() has two parameters; 例如,下面的函数foo()有两个参数。 the first is of type int*
, and the second is of type void(*)(void)
, ie, a pointer-to-function type. 第一个是int*
类型,第二个是void(*)(void)
,即函数指针类型。
void foo(int not_really_an_array[20],
void not_really_a_function(void))
{
}
int main() {
int *int_ptr = 0;
void (*func_ptr)(void) = 0;
foo(int_ptr, func_ptr);
}
The error in the code in the question is not in the declaration of leeftijd_in_maanden()
, it's in the call: 问题中代码中的错误不在leeftijd_in_maanden()
的声明中,而是在调用中:
...
<< leeftijd_in_maanden( leeftijd_in_jaren(geboortejaar, geboortemaand, geboortedag),
geboortejaar, geboortemaand, geboortedag)
...
The first argument, leeftijd_in_jaren(geboortejaar, geboortemaand, geboortedag)
, has the form of a function call ; 第一个参数leeftijd_in_jaren(geboortejaar, geboortemaand, geboortedag)
具有函数调用的形式; since the function returns int
, it's an expression of type int
, not the pointer-to-function expected by leeftijd_in_maanden()
. 由于该函数返回int
,因此它是int
类型的表达式,而不是leeftijd_in_maanden()
期望的函数指针。
Instead, you need to pass just the name of the function (which is implicitly converted to a pointer to the function): 相反,您只需要传递函数的名称 (隐式转换为指向函数的指针):
...
<< leeftijd_in_maanden( leeftijd_in_jaren,
geboortejaar, geboortemaand, geboortedag)
...
If you make that change (it's on line 50), the program compiles. 如果进行了更改(在第50行),程序将编译。 (I can't tell whether it's semantically correct because I can't read any of the identifiers; they're in Dutch, right?) (由于无法读取任何标识符,因此我无法确定它在语义上是否正确;它们是荷兰语,对吗?)
A function declaration is not a valid parameter for a function. 函数声明不是函数的有效参数。 Also, it is completely unnecessary in your case, as all you do is just call leeftijd_in_jaren
, which is already visible in the scope of leeftijd_in_maanden
. 另外,对于您而言,这完全没有必要,因为您只需调用leeftijd_in_jaren
,这已经在leeftijd_in_maanden
的范围内leeftijd_in_maanden
。
Please try to post an actually useful testcase next time. 请尝试下一次发布一个实际有用的测试用例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.