0

I'm trying to sort a list of a custom struct type, with a custom function I have write, but give me an error:

Use of undeclared identifier

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
}

This is the error:

enter image description here

beacuse seem that expects input parameters...

Piero
  • 9,173
  • 18
  • 90
  • 160

1 Answers1

0

This code compiles fine for me. Have you included the list from std? This is how I am building it: 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;
}
László Papp
  • 51,870
  • 39
  • 111
  • 135