C++ 使用结构体时,表达式必须具有 (Pointer-to-) 函数类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12232183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expression must have (Pointer-to-) function type while using structs
提问by Painguy
So basically what I'm doing is passing an array of structs from one function to another function. It works for the show functions, but not for the max functions :/ What exactly am i doing wrong?
所以基本上我正在做的是将一个结构数组从一个函数传递到另一个函数。它适用于显示功能,但不适用于最大功能:/我到底做错了什么?
void show( const ABC & x ){
cout<<"{"<<x.n<<",'"<<x.c<<"',{"<<x.a[0]<<","<<x.a[1]<<","<<x.a[2]<<"}}";
}
void show( const ABC arr[], unsigned elements ){
for(unsigned i=0; i<elements; i++)
show(arr[i]);
}
the following doesn't work
以下不起作用
double max( const ABC & x ){
double max=x.a[2];
if(x.a[1]>max)
max=x.a[1];
if(x.a[0]>max)
max=x.a[0];
return max;
}
double max( const ABC arr[], unsigned elements ){
double max=arr[2].a[3];
for(unsigned i=0; i<elements; i++)
if(max<max(arr[i])){
max=max(arr[i]);
}
return max;
}
回答by Luc Danton
Rename your double
variable to something else than max
. As things are, it is hiding the functions that share the same name, hence why the call expression is invalid (you're trying to pass arguments to a double
).
将您的double
变量重命名为max
. 事实上,它隐藏了共享相同名称的函数,因此调用表达式无效(您试图将参数传递给 a double
)。