错误:表达式不能用作函数.如何删除它
Error: expression cannot be used as a function. How to remove this?
我正在使用这个结构
struct box
{
int h,w,d;
};
int compare (const void *a, const void * b)
{
return ((*(box *)b).d * (*(box *)b).w) – ((*(box *)a).d * (*(box *)a).w); // error is showing in this line
}
box rot[3*n];
qsort (rot, n, sizeof(rot[0]), compare);
我正在尝试 qsort但显示错误表达式不能用作函数
返回行中的减号运算符有问题,应该-
而不是–
定义数组时的另一个问题rot
数组应包含类型struct box
而不是box
的元素,因为结构块中没有typedef
因此,您面临两种使其工作的可能性,要么为结构box
添加一个标签box
:
typedef struct box
{
int h,w,d;
} box;
或者简单地在数组的定义中添加单词 struct
rot
并在比较函数中像这样(在每个box
单词之前):
int compare (const void *a, const void * b)
{
return ((*(struct box *)b).d * (*(struct box *)b).w) - ((*(struct box *)a).d * (*(struct box *)a).w);
}
和
struct box rot[3*n];
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 擦除删除成语的性能增益从何而来
- 重载运算符 new/new[] 删除/删除[] 全局C++
- 使用删除 [] 删除由 new [] 创建的数组
- BST 删除/删除节点 - 根
- C++ 使用删除 [] 删除指针
- 删除/删除矢量项的最有效/最快方法
- 删除删除/删除[]
- 使用 std::删除删除错误的字符