Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6625547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:47:16+00:00 2026-05-25T21:47:16+00:00

I have a graph class that has a vector of nodes. In each node,

  • 0

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.

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.

So I am trying to figure out STL sort, but I’m having difficulties.

I have

 std::sort(path.begin(), path.end());

Where path is the vector of nodes (node = vertex value and list of edges)

In my node class, I have

bool operator<(const Node<T>& rhs){
    return size < rhs.size; //size is how many edges the vertex has
}

But it’s giving me errors. How can I construct the operator< function in my node class to work with STL sort?

Here are the errors:

$ 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&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >, _Tp = Node<std::basic_string<char> >]':
/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<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >]'
/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<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >, _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<Node<std::basic_string<char> >*, std::vector<Node<std::basic_string<char> >, std::allocator<Node<std::basic_string<char> > > > >]'
graph.h:32:13:   instantiated from 'void Graph<T>::topN(int) [with T = std::basic_string<char>]'
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<std::basic_string<char> >' as 'this' argument of 'bool Node<T>::operator<(const Node<T>&) [with T = std::basic_string<char>]' discards qualifiers
make: *** [graphBuilder.o] Error 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:47:16+00:00Added an answer on May 25, 2026 at 9:47 pm

    Your member function needs to be const-qualified:

    bool operator<(const Node<T>& rhs) const{
    

    EDIT

    Per request, here is a bit more how I knew that you needed to make the member function const.

    Divining the hidden meanings in stdlib-related compiler errors is something of an art, and something that you get better at with practice. Stdlib-related errors will often emit a whole series of compiler errors. Usually the most useful of these errors is the last one, because that one is generated from the context of the code you actually wrote, rather than coming from the bowels of the library code.

    In this case, the last compiler error was:

    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 >’ as ‘this’
    argument of ‘bool Node::operator<(const Node&) [with T =
    std::basic_string]’ discards qualifiers

    I’ve highlighted the illuminating bits. This tells me that in OP’s actual code, because of how the code is constructed (maybe the sort call is itself in a const member function? who knows…) the this pointer must be const, but as we can see from the posted declaration of operator<, the method is not const. The “qualifiers” referred to in the compiler errors are what the Standard calls “cv-qualifiers”. “cv” stands for “const/volatile.”

    When the compiler says “Passing const X as this to Node::operator< discards qualifiers” what it’s really trying to say is:

    “You said X was const but then you tried to call a non-const member function through const X. In order for me to make this call, I would have to discard the const qualifier on X. I’m not allowed to do that, so you have to fix your code.”

    The qualifiers being “discarded” here are the qualifiers on the method itself. In other words, operator< must be a const member function, but it’s not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that represents undirected edges in a graph. Every edge has
In Python, I have a Graph class that has a dictionary of vertex objects.
I have a class Graph with two lists types namely nodes and edges I
I have Vertex template in vertex.h. From my graph.h: 20 template<class edgeDecor, class vertexDecor,
I have a class (in C++), call it Data , that has thousands of
Assume we have a Menu class that has SubMenus (which is the same type
I have a class that does an implementation of a graph (template class) in
I have a graph with n nodes as an adjacency matrix . Is it
I have oriented graph. Graph can be strongly connected. Every vertex can have a
I have code which will draw a graph that scales if the user attempts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.