const confusion

This is a discussion on const confusion within the C++ Programming forums, part of the General Programming Boards category; Code: prototypes: void edit_book(map<string,Book> *); map<string,Book> *get_ptrLibrary() const {return ptrLibrary;}; call to function: error here edit_book(get_ptrLibrary()); I am getting the ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    const confusion

    Code:
    prototypes:
    void edit_book(map<string,Book> *);
    map<string,Book> *get_ptrLibrary() const {return ptrLibrary;};
    
    
    call to function: error here
    edit_book(get_ptrLibrary());
    I am getting the following error:
    editmenu.cpp:46: passing `const EditMenu' as `this' argument of `void
    EditMenu::edit_book(std::map<std::string, Book, std::less<std::string>,

    std::allocator<std::pair<const std::string, Book> > >*)' discards qualifiers

    I think this has to do with my const declaration
    I want edit_book(map<string,Book> *) to be able to change the map but get_ptrLibrary() shouldn't alter anything therefore is a const function. What exactly is the problem here?

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Post the whole class & function where error happens code please.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    The whole file:
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::left;
    
    #include <iomanip>
    using std::setw;
    
    #include <map>
    using std::map;
    
    #include "menu.h"
    #include "book.h"
    #include "editmenu.h"
    
    EditMenu::EditMenu(){}
    
    EditMenu::EditMenu(std::map<string,Book> & rlibrary){
        ptrLibrary=&rlibrary;
    }
    
    void EditMenu::display_menu() const{
        cout << "\t\tEdit Menu\n";
        cout << "1.  Display Library\n";
        cout << "2.  Edit Book\n";
        cout << "3.  Remove Book\n";
        cout << "4.  Add Book\n";
        cout << "5.  Display Book\n";
        cout << "6.  Main Menu"<<endl;
    }
    
    bool EditMenu::validate_option(int noption) const{
        bool valid;
        if (noption>=0 && noption<=6)
            valid=TRUE;
        else
            valid=FALSE;
        return valid;
    }
    void EditMenu::process_option(int noption) const{
        switch (noption){
                case 1: display_library(get_ptrLibrary());
                        break;
                        
                case 2: {
                            edit_book(get_ptrLibrary());  
                        }
                        break;
                        
                case 3:                         
                case 4:
                case 5:
                case 6:
                default:
                        break;
                    
        }
    }
    void EditMenu::display_library(map<string,Book> *ptrLib) const{
        cout << setw(8)<<left<<"ISBN"
             << setw(24)<<left<<"  Title"<<endl;
        
        map<string,Book>::iterator pos;
        for (pos=ptrLib->begin();pos !=ptrLib->end();++pos){
            cout << "--------------------------------------------\n";
            cout << setw(10)<<left<<pos->second.get_ISBN()
                 << setw(60)<<left<<pos->second.get_title()<<endl;
        }   
    }
    
     
    
    void EditMenu::remove_book(map<string,Book> *ptrLib){}
    void EditMenu::add_book(map<string,Book> *ptrLib){}
    void EditMenu::edit_book(map<string,Book> *ptrLib){
    string ttitle;
        cout << "Enter Book Title: ";
        cin >> ttitle;
        cin.ignore();
        map<string,Book>::iterator pos;
        pos=ptrLib->find(ttitle);
        cout <<pos->second;
        cin >>pos->second;
        cout endl;
        
    }

  4. #4
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    You are calling a non-const member function:
    Code:
    void EditMenu::edit_book(map<string,Book> *ptrLib)
    From a const function:
    Code:
    void EditMenu::process_option(int noption) const
    That is, in a const function, the this pointer is const too, so, passing it to a non-const function makes it lose its qualifiers.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Darn, I guess I am going to have to edit process_option to be non constant from my base class to my derived classes. Should have thought of this in the beginning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21