-
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?
-
Post the whole class & function where error happens code please.
-
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;
}
-
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.
-
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.