Hi everyone. I’m having some problems:mad: debugging the following code:

typedef map<string, map<string, Person> > pclass;
   pclass::const_iterator i;
   map<string, Person>::const_iterator iter; 
   pclass person_map;
    
   void Driver::add_record()
   {
        string surname, name, phone, age, weight;
        int new_age;
        double new_weight;
        
        cout << "Enter name: ";
        cin >> name;
        cout << "\nEnter surname: ";
        cin >> surname;
        cout << "\nEnter phone number: ";
        cin >> phone;
        cout << "\nEnter age: ";
        cin >> age;
        try {
            new_age = check_age(age);
   253   iter->second.read_age(new_age); 
        } catch (range_error e) {
          cout << "An error has occured: " << e.what() << endl;
    
   for (i = person_map.begin(); i != person_map.end(); ++i) {
            if (i->first == surname) {
               for (iter = i->second.begin(); iter != i->second.end(); ++iter) {
                   if (iter->first == name) {

where the read_age() function is declared within my Person class:

//Person class declaration
   class Person {
   public:
          std::istream& read_surname(std::istream&);
          std::istream& read_name(std::istream&);
          std::istream& read_phone(std::istream&);
          std::istream& read_age(std::istream&);
          std::istream& read_weight(std::istream&);
          std::string& read_phone(std::string&);
          int& read_age(int&);
          double& read_weight(double&);
          std::string get_name() const { return name; }
          std::string get_surname() const { return surname; }
          std::string get_phone() const { return phone; }
          int get_age() const { return age; }
          double get_weight() const { return weight; }
          Person(std::string&, std::string&, std::string&, int&, double&);
          Person(std::istream&);
   private:
           std::string surname;
           std::string name;
           std::string phone;
           int age;
           double weight;
       };    
   #endif

and the function definition:

//Accessor Function to read integer input into age variable
   int& Person::read_age(int& a)
   {
       a >> age;
       return a;
   }

Here’s the error I receive:

253 driver.cpp passing `const Person' as `this' argument of `int& Person::read_age(int&)' discards qualifiers

If someone could please help me out that would be awesome!

I’m running Dev C++ 4.9.9.2 on Windows XP(Service Pack 2)

Here’s a short section of my compiler log:

Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Steven\Desktop\CTI\C++ Unit 3\Finished Project\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Steven\Desktop\CTI\C++ Unit 3\Finished Project\Makefile.win" all
g++.exe -c driver.cpp -o driver.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

driver.cpp: In member function `void Driver::add_record()':
driver.cpp:253: error: passing `const Person' as `this' argument of `int& Person::read_age(int&)' discards qualifiers

P.S. I hope the info at hand is enough.

Why can't you just use the = operator?

//Accessor Function to read integer input into age variable
   int& Person::read_age(int& a)
   {
       a = age;
       return a;
   }

That's a good point WolfPack! I was using the '>>' because I copied the function from a function using an istream for input. Never occured to me to use the '=' operator. It still doesn't resolve the error, but thanx for the advice though.

I think the problem is that you have declared iter as const_iterator.

map<string, Person>::const_iterator iter;

Change it to

map<string, Person>::iterator iter;

That makes sense... Thanx so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.