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 3720720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:18:40+00:00 2026-05-19T05:18:40+00:00

I can get a method of a class in a set iterator ? #include

  • 0

I can get a method of a class in a set iterator ?

#include <iostream>
#include <string>
#include <set>
class student{
  public:
     student(std::string n){
        name=n;
     }
     void print(){
        std::cout << name << std::endl;
     }
     bool operator < (const student & s1){ return true;}
     bool operator = (const student & s1){ return true;}
  private:
     std::string name;
};
int main(){
  std::set<student> studs;
  studs.insert(student("name01"));
  studs.insert(student("name02"));
  std::set<student>::iterator it;
  for(it = studs.begin(); it != studs.end(); it++)
      (*it).print() ;
}

I get this error

students.cpp: In function ‘int main()’:  
students.cpp:22: error: passing ‘const student’ as ‘this’ argument of ‘void student::print()’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = student]’:
/usr/include/c++/4.2.1/bits/stl_tree.h:982:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = student, _Val = student, _KeyOfValue = std::_Identity<student>, _Compare = std::less<student>, _Alloc = std::allocator<student>]’
/usr/include/c++/4.2.1/bits/stl_set.h:307:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = student, _Compare = std::less<student>, _Alloc = std::allocator<student>]’
students.cpp:18:   instantiated from here
/usr/include/c++/4.2.1/bits/stl_function.h:227: error: passing ‘const student’ as ‘this’ argument of ‘bool student::operator<(const student&)’ discards qualifiers

with

     bool operator<(const student & s1) const { return true;}  
     bool operator==(const student & s1) const { return true;}  

now work!! O_o’,

#include <iostream>
#include <string>
#include <set>
class student{
  public:
     student(std::string n){
        name=n;
     }
     void print() const {
        std::cout << name << std::endl;
     }
     bool operator<(const student & s1) const { return true;}
     bool operator==(const student & s1) const { return true;}
  private:
     std::string name;
};
int main(){
  std::set<student> studs;
  studs.insert(student("name01"));
  studs.insert(student("name02"));
  std::set<student>::iterator it;
  for(it = studs.begin(); it != studs.end(); it++)
      it->print() ;
}
  • 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-19T05:18:41+00:00Added an answer on May 19, 2026 at 5:18 am

    You need to add a const qualifer to your print member function:

    void print() const
    {
      std::cout << name << std::endl;
    }
    

    Objects in an std::set are necessarily const, since they are used as keys. When an object (or reference) is constant, you can only call member functions of that object which are declared with the const qualifier.

    You also want const qualifiers on both the == and < operator overload functions. (And don’t forget to change = to == as pointed out in the comments.)

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

Sidebar

Related Questions

I know I can get the public static members of a class by doing
Ruby can add methods to the Number class and other core types to get
I can get easily see what projects and dlls a single project references from
I can get both System.Net.Mail and System.Web.Mail to work with GMail, but I can't
Logging can get complicated, quickly. Considering that you have some code, how do you
I can get the executable location from the process, how do I get the
I can get simple examples to work fine as long as there's no master
I can get the element like this $(#txtEmail) but I'm not sure how to
I can get a list of running threads from Process.GetCurrentProcess().Threads, but I need to
I CAN get the associated icon just fine, and draw it accordingly, however, I

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.