Note that there are some explanatory texts on larger screens.

plurals
  1. POSTL Sort on nested Classes
    primarykey
    data
    text
    <p>I have a graph class that has a vector of nodes. In each node, there is a vertex and a STL list of edges. Essentially it's an adjacency list.</p> <p>For my assignment, I am trying to display the vertices with the most edges. I figured I would sort the graph's vector of nodes by edge size and print the top N vertices.</p> <p>So I am trying to figure out STL sort, but I'm having difficulties.</p> <p>I have </p> <pre><code> std::sort(path.begin(), path.end()); </code></pre> <p>Where path is the vector of nodes (node = vertex value and list of edges)</p> <p>In my node class, I have </p> <pre><code>bool operator&lt;(const Node&lt;T&gt;&amp; rhs){ return size &lt; rhs.size; //size is how many edges the vertex has } </code></pre> <p>But it's giving me errors. How can I construct the <code>operator&lt;</code> function in my node class to work with STL sort?</p> <p>Here are the errors:</p> <pre><code>$ make g++ -c -g -std=c++0x graphBuilder.cpp In file included from /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/algorithm:63:0, from graph.h:6, from graphBuilder.cpp:2: /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&amp;) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;Node&lt;std::basic_string&lt;char&gt; &gt;*, std::vector&lt;Node&lt;std::basic_string&lt;char&gt; &gt;, std::allocator&lt;Node&lt;std::basic_string&lt;char&gt; &gt; &gt; &gt; &gt;, _Tp = Node&lt;std::basic_string&lt;char&gt; &gt;]': /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:2249:70: instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;Node&lt;std::basic_string&lt;char&gt; &gt;*, std::vector&lt;Node&lt;std::basic_string&lt;char&gt; &gt;, std::allocator&lt;Node&lt;std::basic_string&lt;char&gt; &gt; &gt; &gt; &gt;]' /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:2280:54: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator&lt;Node&lt;std::basic_string&lt;char&gt; &gt;*, std::vector&lt;Node&lt;std::basic_string&lt;char&gt; &gt;, std::allocator&lt;Node&lt;std::basic_string&lt;char&gt; &gt; &gt; &gt; &gt;, _Size = int]' /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:5212:4: instantiated from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator&lt;Node&lt;std::basic_string&lt;char&gt; &gt;*, std::vector&lt;Node&lt;std::basic_string&lt;char&gt; &gt;, std::allocator&lt;Node&lt;std::basic_string&lt;char&gt; &gt; &gt; &gt; &gt;]' graph.h:32:13: instantiated from 'void Graph&lt;T&gt;::topN(int) [with T = std::basic_string&lt;char&gt;]' graphBuilder.cpp:10:17: instantiated from here /usr/lib/gcc/i486-slackware-linux/4.5.2/../../../../include/c++/4.5.2/bits/stl_algo.h:2211:4: error: passing 'const Node&lt;std::basic_string&lt;char&gt; &gt;' as 'this' argument of 'bool Node&lt;T&gt;::operator&lt;(const Node&lt;T&gt;&amp;) [with T = std::basic_string&lt;char&gt;]' discards qualifiers make: *** [graphBuilder.o] Error 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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