c++ - 关于 c++ 中的函数 typedef
问题描述
typedef bool list_less_func (const struct list_elem *a,
const struct list_elem *b,
void *aux);
void
list_sort (struct list *list, list_less_func *less, void *aux)
{
size_t output_run_cnt; /* Number of runs output in current pass. */
ASSERT (list != NULL);
ASSERT (less != NULL);
/* Pass over the list repeatedly, merging adjacent runs of
nondecreasing elements, until only one run is left. */
do
{
struct list_elem *a0; /* Start of first run. */
struct list_elem *a1b0; /* End of first run, start of second. */
struct list_elem *b1; /* End of second run. */
output_run_cnt = 0;
for (a0 = list_begin (list); a0 != list_end (list); a0 = b1)
{
/* Each iteration produces one output run. */
output_run_cnt++;
/* Locate two adjacent runs of nondecreasing elements
A0...A1B0 and A1B0...B1. */
a1b0 = find_end_of_run (a0, list_end (list), less, aux);
if (a1b0 == list_end (list))
break;
b1 = find_end_of_run (a1b0, list_end (list), less, aux);
/* Merge the runs. */
inplace_merge (a0, a1b0, b1, less, aux);
}
}
while (output_run_cnt > 1);
ASSERT (is_sorted (list_begin (list), list_end (list), less, aux));
}
void wordcount_sort(word_count_list_t *wclist,
bool less(const word_count_t *, const word_count_t *)) {
list_sort(wclist, less_list, less);
}
static bool less_list(const struct list_elem *ewc1,
const struct list_elem *ewc2, void *aux) {
/* TODO */
list_less_func* comparefunc;
if (comparefunc(ewc1, ewc2, aux))
return false;
else
return true;
}
嘿伙计们,我认为这是一个简单的 C++ 问题。问题出在 less_list(...) 函数中,应该是关于函数 typedef 的问题。我对此并不熟悉,但我的截止日期快到了。感谢帮助!而且你可以忽略list_sort中的大部分代码,重要信息只是“少”功能。
解决方案
您正在调用未初始化的函数指针。这会导致未定义的行为。您必须定义一个函数并将函数的地址分配给函数指针:
struct list_elem {};
typedef bool list_less_func (const list_elem *a,
const list_elem *b,
void *aux);
list_less_func f;
int main() {
const list_elem *ewc1 = nullptr;
const list_elem *ewc2 = nullptr;
void *aux = nullptr;
list_less_func* comparefunc = f;
comparefunc(ewc1, ewc2, aux);
}
bool f (const list_elem *a,
const list_elem *b,
void *aux) {
return a && b && aux;
}
该函数f
只是一个示例。你必须实现一个更少的功能。
无关:
代替
if (comparefunc(ewc1, ewc2, aux))
return false;
else
return true;
你可以写
return !comparefunc(ewc1, ewc2, aux);
推荐阅读
- python - Python变量格式在IF内部发生变化,而第一个条件是好的
- java - Java shebang 应用程序中的图标资源
- sql - 制作一个 Postgresql 查询,它只返回与引用同一对象的前一行相比值发生变化的行
- python - Django 转换为原始查询
- reactjs - React useState 不更新提交表单上的状态
- c# - C# 中的简单工厂模式
- ftdi - 使用来自 C232HM FTDI 的 GPIO
- mysql - 获取Unix时间戳中每个特定时期范围内的平均值,该时间戳在MySQL中的特定条件下返回-1
- android - FAILURE:构建失败并出现异常 (Flutter)
- google-apps-script - 使用 file.makeCopy() 函数复制副本(Google Apps 脚本)