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

  • SEARCH
  • Home
  • 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 4616398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:58:32+00:00 2026-05-22T01:58:32+00:00

#include <iostream> #include <set> using namespace std; class StudentT { public: int id; string

  • 0
#include <iostream>
#include <set>

using namespace std;

class StudentT {

public:
    int id;
    string name;
public:
    StudentT(int _id, string _name) : id(_id), name(_name) {
    }
    int getId() {
        return id;
    }
    string getName() {
        return name;
    }
};

inline bool operator< (StudentT s1, StudentT s2) {
    return s1.getId() < s2.getId();
}

int main() {

    set<StudentT> st;
    StudentT s1(0, "Tom");
    StudentT s2(1, "Tim");
    st.insert(s1);
    st.insert(s2);
    set<StudentT> :: iterator itr;
    for (itr = st.begin(); itr != st.end(); itr++) {
        cout << itr->getId() << " " << itr->getName() << endl;
    }
    return 0;
}

In line:

cout << itr->getId() << " " << itr->getName() << endl;

It give an error that:

../main.cpp:35: error: passing ‘const StudentT’ as ‘this’ argument of ‘int StudentT::getId()’ discards qualifiers

../main.cpp:35: error: passing ‘const StudentT’ as ‘this’ argument of ‘std::string StudentT::getName()’ discards qualifiers

What’s wrong with this code?

  • 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-22T01:58:33+00:00Added an answer on May 22, 2026 at 1:58 am

    The objects in the std::set are stored as const StudentT. So when you try to call getId() with the const object the compiler detects a problem, mainly you’re calling a non-const member function on const object which is not allowed because non-const member functions make NO PROMISE not to modify the object; so the compiler is going to make a safe assumption that getId() might attempt to modify the object but at the same time, it also notices that the object is const; so any attempt to modify the const object should be an error. Hence compiler generates an error message.

    The solution is simple: make the functions const as:

    int getId() const {
        return id;
    }
    string getName() const {
        return name;
    }
    

    This is necessary because now you can call getId() and getName() on const objects as:

    void f(const StudentT & s)
    {
         cout << s.getId();   //now okay, but error with your versions
         cout << s.getName(); //now okay, but error with your versions
    }
    

    As a sidenote, you should implement operator< as :

    inline bool operator< (const StudentT & s1, const StudentT & s2)
    {
        return  s1.getId() < s2.getId();
    }
    

    Note parameters are now const reference.

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

Sidebar

Related Questions

#include<iostream> using namespace std; class A { int a; int b; public: void eat()
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <boost/bind.hpp> #include <iostream> using namespace std; using boost::bind; class A { public: void
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
#include<iostream> #include<stdlib.h> #includeheader_complex.h using namespace std; class Complex { private: float real,imag; public: Complex();
In my main.cpp: using namespace std; #include <cstdlib> #include <iostream> #include <set> #include <string>
c++ code -------- #include stdafx.h #include <iostream> using namespace std; class A { public:
#include <iostream> using namespace std; class Object{}; class Connection { public: Connection(Object * _obj);
#include <iostream> #include <string.h> using namespace std; int main() { char firstname[] = Alfred;

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.