Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Usually, only the first error is relevant, so we have this:</p> <pre><code>In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0, from prog.cpp:1: prog.cpp: In function 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]': /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:2192:4: instantiated from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]' /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5252:4: instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char&gt; &gt;, _Compare = ReverseComparator&lt;char&gt;]' prog.cpp:12:49: instantiated from here prog.cpp:6:11: error: 'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private </code></pre> <p>Most of that is simply context, so here's the key part:</p> <pre><code>'bool ReverseComparator&lt;T&gt;::operator()(T, T) [with T = char]' is private </code></pre> <p>Well that's easy to fix:</p> <pre><code>#include &lt;algorithm&gt; #include &lt;string&gt; template &lt;typename T&gt; class ReverseComparator{ public: //forgot this bool operator()(T l, T r){return !(l &lt; r);} }; int main() { std::string str = "HELLO"; ReverseComparator&lt;char&gt; comparator; std::sort(str.begin(), str.end(), comparator); } </code></pre> <p><a href="http://ideone.com/KWtasS" rel="nofollow noreferrer">This compiles fine</a>, however, sorts are based on what's called a "strict weak ordering", and <code>!(l &lt; r)</code> is <em>not</em> a strict weak ordering. Namely, <code>comparitor(a,a)</code> should return <code>false</code>, but yours returns <code>true</code>. Luckily, the idea of sorting this way is already in the standard library under the name <code>std::greater</code>. (<code>std::sort</code> without a comparitor uses <code>std::less</code> by default)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload