0

I have looked up all the previous posts I could find on SO on this error, but my case seems to be distinct. Here is the code under consideration (I have not reproduced the complete source here, but I am guessing what has been provided should suffice for the experts on this forum to figure out the cause):

//.h files

#ifndef KEY_BOARD_H
#define KEY_BOARD_H


 ...

class key_board_checker;

class key_board{

friend class key_board_checker;

...

private:
struct PrivateTag{};

public:
static const key_board& get_instance(); 
key_board(PrivateTag);
~key_board() = default;
...
protected:
static bool has_instance;

private:
key_board (const key_board&) = delete;
key_board& operator = (const key_board&) = delete;

private:
static unique_ptr<key_board> keyboardPtr;

...

};

...

#include "key_board_checker.h"

#endif


#ifndef KEY_BOARD_CHECKER_H
#define KEY_BOARD_CHECKER_H

...

class key_board;

class key_board_checker{

public:
key_board_checker(const key_board& rkb);
~key_board_checker()=default;

protected:

private:
key_board_checker(const key_board_checker&)=delete;
key_board_checker& operator=(const key_board_checker&)=delete;

...
   
private:
const key_board& rkb;

private:
static const unsigned short numalnumidx = 18;

...

private:
//housekeeping
vector<vector<string>>final;


private:
...
void check_keyboard () const;
void step1 () const; 
... 
};

#include "key_board.h"

#endif

//.cpp file
key_board_checker::key_board_checker(const key_board& rkb):rkb(rkb){
...
 check_keyboard();
}

void key_board_checker::check_keyboard() const {

  ... 

  step1();

 return;
}


void key_board_checker::step1()const {

 ...

  final.resize(numalnumidx);
  
...

//main

#include "key_board.h"
#include "key_board_checker.h"

int main (){
 auto const& kbr = key_board::get_instance();
 key_board_checker kbc(kbr);
 return 0;
}

I am perplexed by the following error thrown by the g++ compiler:

key_board_checker.cpp: In member function ‘void key_board_checker::step1() const’:
key_board_checker.cpp:56:27: error: passing ‘const std::vector<std::vector<std::__cxx11::basic_string<char> > >’ as ‘this’ argument discards qualifiers [-fpermissive]
   final.resize(numalnumidx);
                           ^
compilation terminated due to -Wfatal-errors.

What am I missing here?

TIA

Vinod
  • 925
  • 8
  • 9
  • 3
    you are calling a non-const member function from a const member function. This is not possible, because the non-const method modifies the object, the const cannot. Much simpler example `struct foo { void f() {} void g() const { f(); }};` – 463035818_is_not_an_ai Aug 21 '23 at 09:42
  • related https://stackoverflow.com/questions/10765787/passing-const-this-argument-discards-qualifiers-fpermissive, https://stackoverflow.com/questions/5973427/error-passing-xxx-as-this-argument-of-xxx-discards-qualifiers, https://stackoverflow.com/questions/26963510/error-passing-const-as-this-argument-of-discards-qualifiers – 463035818_is_not_an_ai Aug 21 '23 at 09:45
  • You are in a const method and you are trying to resize a vector in your class. I'm not sure why that should be perplexing, `const` means you are not allowed to modify anything, and resizing a vector definitely counts as modifying it. – john Aug 21 '23 at 10:52
  • You *could* use `mutable` to allow the member to be modified from a const member function. Not saying it's a good idea here, just saying it is an option. – Jesper Juhl Aug 21 '23 at 11:06

1 Answers1

2

final is a member variable in key_board_checker and the member function step1() is const qualified which means that it promises to not mutate the key_board_checker instance (that is, it promises to not change any of its (non-mutable) member variables).

However, the only thing you do in the function is to mutate the instance since final.resize() potentially changes final. The std::vector::resize() function is for this reason not const qualified. From within const qualified member functions you can only call const qualified member functions on the member variables and on *this.


A mutable member variable is exempt from the const qualification.

struct Foo {
    void bar() const { // const qualified
        ++x;           // ok even though `bar` is const qualified
        // ++y;        // error: y is not mutable.
    }
    mutable int x = 0; // make it mutable even in const context
    int y = 0;
};
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108