【问题标题】:Use of undeclared identifier in sort list with custom function在具有自定义功能的排序列表中使用未声明的标识符
【发布时间】:2013-08-17 09:07:29
【问题描述】:

我正在尝试使用我编写的自定义函数对自定义结构类型的列表进行排序,但给我一个错误:

使用未声明的标识符

struct myStruct
{
    int x1;
    int x2;
};

bool CompareData(const myStruct& a, const myStruct& b)
{
    if (a.x1 < b.x1) return true;
    if (b.x1 < a.x1) return false;

    // a=b for primary condition, go to secondary
    if (a.x2 < b.x2) return true;
    if (b.x2 < a.x2) return false;

    return false;
}

void sortingList ()
{
    std::list<myStruct> custom_dist;
    //...Fill list
    custom_dist.sort(CompareData); //Here i receive the error
}

这是错误:

因为似乎需要输入参数...

【问题讨论】:

  • 你能发布完整的错误信息吗?
  • 我添加了一个错误的图像...
  • 发布构建日志...
  • 什么是构建日志?
  • 我也刚刚检查过,它使用 Xcode 为我编译。

标签: c++ list sorting


【解决方案1】:

这段代码对我来说编译得很好。您是否包含了标准列表中的列表?这就是我构建它的方式:g++ -Wall main.cpp

#include <list>

struct myStruct
{
int x1;
int x2;
};

bool CompareData(const myStruct& a, const myStruct& b)
{
if (a.x1 < b.x1) return true;
if (b.x1 < a.x1) return false;

// a=b for primary condition, go to secondary
if (a.x2 < b.x2) return true;
if (b.x2 < a.x2) return false;

return false;
}

int main()
{
    std::list<myStruct> custom_dist;
    custom_dist.sort(CompareData);
    return 0;
}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多