C++ std::sort with predicate function in Class
<p>I want to sort vector of certain struct with certain order in certain class. I've wrote definition of struct and predicate function in a class and run std::sort in a method of the class with these struct and function. But compilation error has occurred. gcc version is 4.0.1 and OS is Mac OSX. The code is following:</p>

<pre><code>class sample {
public:
  struct s {
    int x;
    int y;
  };

  bool cmp (struct s a, struct s b) {
    if (a.x == b.x)
      return a.y &lt; b.y;
    else
      return a.x &lt; b.x;
  }

  int func(void) {
    std::vector &lt;struct s&gt; vec;

    // ...

    sort(vec.begin(), vec.end(), cmp);  // compilation error

    // ...

    return 0;
  }
};

int main(void) {
  sample *smp = new sample();
  smp-&gt;func();
  return 0;
}
</code></pre>

<p>Error message was huge and complex. So this is first two lines of it.</p>

<blockquote>
  <p>sortSample.cpp: In member function 'int sample::func()':<br />
  sortSample.cpp:51: error: argument of type 'bool (sample::)(sample::s, sample::s)' does not match 'bool (sample::*)(sample::s, sample::s)'<br />
  ...</p>
</blockquote>

<p>Instead of above approach, the code could run correctly with following ways.</p>

<ol>
<li>Define <code>struct s</code> and function <code>cmp()</code>
outside of <code>class sample</code>. </li>
<li>Remove
function <code>cmp()</code> and define operator
overloading of <code>&lt;</code> in <code>struct s</code>.</li>
</ol>

<p>Sample code of each approach is bellow.</p>

<p>1)</p>

<pre><code>struct s {
  int x;
  int y;
};

bool cmp (struct s a, struct s b) {
  if (a.x == b.x)
    return a.y &lt; b.y;
  else
    return a.x &lt; b.x;
}

class sample {
// ...
</code></pre>

<p>2)</p>

<pre><code>struct s {
  int x;
  int y;

  bool operator&lt;(const struct s &amp; a) const {
    if (x == a.x)
      return y &lt; a.y;
    else
      return x &lt; a.x;
  }
};
</code></pre>

<p>Can anyone tell a mechanism of this behavior? Why does first approach invokes compilation error?</p>

<p>Thanks.</p>
