• Register
1 vote
469 views

Problem:

Hello Kodlogs,

I find you guys very helpful and friendly alongside. You people are helping each other proactively. It’s nice to see. I have a simple piece of code written in C++ that is producing an error passing as this argument discards qualifiers. I can smell something very silly but I couldn’t find out the problem in it. Could any of you please grab me up from this error by fixing it? I am attaching my code snippet below for your better understanding.

using namespace std;

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

inline bool operator< (Employee e1, Employee e2) {
    return  e1.getId() < e2.getId();
}

int main() {
    set<Employee> obj;
    Employee e1(0, "Roy");
    Employee e2(1, "Joy");
    obj.insert(e1);
    obj.insert(e2);
    set<Employee> :: iterator itr;
    for (itr = obj.begin(); itr != obj.end(); itr++) {
        cout << itr->getId() << " " << itr->getName() << endl;
    }
    return 0;
}

I must recognize your concern. Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

You need to understand The objects in the std::set are stored as const Employee. Once you tried to call the getId() function with the const object, the compiler encountered an error. Actually you are trying to call a non-const member function on a const object. The C++ compiler doesn't allow you to do that. So, to prevent the error, make the function as a const

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

And now the compile will allow you to call on a const object. I am sure this must solve the problem. If it still doesn’t work in your case, please let me know. Thanks. 

13 9 6
94,240 points

Related questions

0 votes
1 answer 10 views
10 views
Problem: Hello! I am a noob. I came to know about optional parameters in C++. My concept of optional parameters is not clear. Moreover, I read somewhere that in a function definition, an optional parameter must be placed first in the argument list. Is this true? Can anybody explain the concept of default arguments to me? Thank you.
asked Dec 4, 2020 Code Learner 6.5k points
0 votes
1 answer 7 views
7 views
Problem: I get some error that I can't figure out. Any clue what is wrong with my sample code? class B: def meth(self, arg): print arg class C(B): def meth(self, arg): super(C, self).meth(arg) print C().meth(1) I got the sample test code from help of 'super' built-in method.
asked Feb 23 Mashhoodch 10.2k points
0 votes
1 answer 32 views
32 views
Problem: I am learning Python. Recently I got an assignment in which I had to replace some characters of a string with other characters. While searching for the solution on the Internet, I came across the translate() method that can be used to do so. I tried the ... print(string.translate(string ,table)); I do not know why this problem occurs. Can anybody here help me sort out this error? Thanks!
asked Dec 27, 2020 Code Learner 6.5k points
1 vote
1 answer 131 views
131 views
Problem: Hello Kodlogs, I am going through a problem for the last few minutes. I tried to solve it in many ways but still couldn&rsquo;t get rid of this. I&rsquo;ve learning c++ programming language from last week. So, you can treat me as a novice in it. I have a ... (addr,retstring().c_str()); And the function I tried to call: void Printfunc(int a, char *loc, char *stream) Thanks. Good day!
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 21 views
21 views
Problem: I am doing a C ++ wrapper for a C library. I don't want to include the C headers within them .hpp, so I work with type predeclaration. Also, trying not to commit too many undefined behaviors, most of my pointers are void *, and I am shaping as necessary. I have a curious ... public Base< uv_loop_t * > { bool test( ) const { return uv( ); } }; What am I doing wrong ? How do I solve it ?
asked Feb 7 sasha 13.2k points
0 votes
1 answer 110 views
110 views
Problem: Hi there! I just took an exam of programming in which there was a multiple-choice question that said: &ldquo;When an array is passed to a function, it is interpreted as _____&rdquo;: Address of the first element of the array. The number of elements of the array. Address ... 5,4,3,2,1. I did not understand the output. Can anybody please explain it? Also, tell the answer to the above MCQ.
asked Dec 24, 2020 Code Learner 6.5k points
1 vote
1 answer 108 views
108 views
Problem: For the following code I got some error message like: ValueError: not enough values to unpack (expected 3, got 2) The code: import openpyxl, smtplib, sys wbs = openpyxl.load_workbook('duesRecords.xlsx') sheet = wb.get_sheet_by_name('Sheet1') lastCol = sheet. ... problem sending email to %s: %s' % (email, sendmailStatus)) smtpObj.quit() Can anyone help me out to solve this problem?
asked Apr 8, 2020 LizzyM 6.1k points
1 vote
1 answer 153 views
153 views
Problem: I am getting an error while I am trying to run a python program. Please have a look at it and give me a solution if possible. I am trying to figure the problem but I fail to do so. from sys import argv script, usr_name =argv prompt = '>' ... more than 1 value to unpack&rdquo; Traceback (most recent call last): script, usr_name =argv ValueError: need more than 1 value to unpack Thanks.
asked Mar 28, 2020 Gavin 15.3k points
0 votes
1 answer 15 views
15 views
Problem Hey, want to ask you about my problem regarding a file passing to a function. I was trying to create a file which will ask to user for its path then passing it as an argument to a new input/ output then passing that file as a reference to update file ... ::ifstream OutputFile (FilePath);     std::ofstream InputFile (FilePath);          ModifyFile(OutputFile, InputFile);     return 0; }
asked Jan 12 chris jordan 2.4k points