Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,everyone,I have encountered an error when I use map's const_iterator,following are my codes,please help me,thank you very much!

Employ.cc
C#
#include <string>
#include <iostream>
using namespace std;
class Employ{
    friend class EmployCompare;
public:
    Employ(const string& firstname,const string& lastname):firstname_(firstname),lastname_(lastname){}
    string getFirstName(){return firstname_;}
    string getLastName(){return lastname_;}
private:
    string lastname_;
    string firstname_;
};
class EmployCompare{
public:
    bool operator()(const Employ& e1,const Employ e2) const{
        if(e1.lastname_ < e2.lastname_){
            return true;
        }
        else if(e1.lastname_ == e2.lastname_){
            return (e1.firstname_ < e2.firstname_);
        }
        else{
            return false;
        }
    }
};


main.cc
XML
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include "Employ.cc"
using namespace std;
int main( )
{
    map<Employ,string,EmployCompare> employMap;
    Employ em1("lily","beautiful"),em2("boge","hardwork"),em3("relation","love"),em4("result","nice");
    employMap[em1] = "tester";
    employMap[em2] = "programmer";
    employMap[em3] = "coder";
    employMap[em4] = "student";
    map<Employ,string,EmployCompare>::const_iterator it = employMap.begin();
    while(it != employMap.end()){
        cout << it->first.getFirstName() << "--" << it->first.getLastName() << " is " << it->second << endl;
    }
    return 0;
}


And the error:
VB
../src/main.cc:21: error: passing ‘const Employ’ as ‘this’ argument of ‘std::string Employ::getFirstName()’ discards qualifiers
../src/main.cc:21: error: passing ‘const Employ’ as ‘this’ argument of ‘std::string Employ::getLastName()’ discards qualifiers


Thank you very much!
Posted

xcnxx seems to have the right answer - but let me explain a little.

First of all - you encounter the errors because you are trying to use a const_iterator to call methods which are not const. Obviously there are two solutions: either, as xcnxx suggested, use a regular iterator, or, and if I'm not mistaken this is actually closer to what you intended to do - declare the methods as const. To do that simply add the const keyword at the end of each method's declaration/definition. See here for a simple example: wiki[^]

Please note however, that declaring the methods as const prohibits you from changing the object inside of the method - not a problem in this particular case, but something to bare in mind.

Good luck
 
Share this answer
 
Comments
tiancehngbo 14-Jul-11 1:12am    
Thanks for you help,the second one is what I want~
hi! my answer is here!

XML
#include <iostream>
#include <vector>
#include "Employ.h"
using namespace std;

int main( )
{

    Employ em1("lily","beautiful"),em2("boge","hardwork"),em3("relation","love"),em4("result","nice");
    vector<Employ> vec;
    vec.push_back(em1);
    vec.push_back(em2);
    vec.push_back(em3);
    vec.push_back(em4);

    for(vector<Employ>::iterator it = vec.begin();it!=vec.end();it++)
    {
        cout << it->getFirstName() << "--" << it->getLastName() << endl;
    }
    return 0;
}
 
Share this answer
 
Comments
tiancehngbo 14-Jul-11 0:41am    
thank you very much,your solution is ok,but what I want to try is using map,another person give me a sugesstion:
string getFirstName()const{return firstname_;}
string getLastName()const {return lastname_;}
and this works well,thank you all the same!
Philippe Mori 16-Jul-11 18:58pm    
This is generally the preferred solution...

As soon as someone uses const, it must be used consistently...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900