94

error: passing 'const A' as 'this' argument of 'void A::hi()' discards qualifiers [-fpermissive]

I don't understand why I'm getting this error, I'm not returning anything just passing the reference of the object and that is it.

#include <iostream>

class A
{
public:
    void hi()
    {
        std::cout << "hi." << std::endl;
    }
};

class B
{
public:
    void receive(const A& a) {
        a.hi();
    }
};

class C
{
public:
    void receive(const A& a) {
        B b;
        b.receive(a);
    }
};

int main(int argc, char ** argv)
{
    A a;
    C c;
    c.receive(a);

    return 0;
}

@edit

I fixed it using const correctness but now I'm trying to call methods inside of the same method and I get the same error, but the weird thing is that I'm not passing the reference to this method.

#include <iostream>

class A
{
public:
    void sayhi() const
    {
        hello();
        world();
    }

    void hello()
    {
        std::cout << "world" << std::endl;
    }

    void world()
    {
        std::cout << "world" << std::endl;
    }
};

class B
{
public:
    void receive(const A& a) {
        a.sayhi();
    }
};

class C
{
public:
    void receive(const A& a) {
        B b;
        b.receive(a);
    }
};

int main(int argc, char ** argv)
{
    A a;
    C c;
    c.receive(a);

    return 0;
}

error: passing 'const A' as 'this' argument of 'void A::hello()' discards qualifiers [-fpermissive]

error: passing 'const A' as 'this' argument of 'void A::world()' discards qualifiers [-fpermissive]

Community
  • 1
  • 1
yayuj
  • 2,194
  • 3
  • 17
  • 29

3 Answers3

115

Your hi method is not declared as const inside your A class. Hence, the compiler cannot guarantee that calling a.hi() will not change your constant reference to a, thus it raises an error.

You can read more about constant member functions here and correct usage of the const keyword here.

bigjosh
  • 1,273
  • 13
  • 19
therealrootuser
  • 10,215
  • 7
  • 31
  • 46
  • 4
    I changed to `void hi() const` and now it's working. Is it right? a void with const correctness? – yayuj Nov 17 '14 at 00:03
  • 6
    Yes. The const tells the compiler that nothing will change inside that function. The return type is not relevant to this – Eric Nov 17 '14 at 00:05
  • 3
    In this case, since `a.hi()` does not change the object, this would be correct. It also helps inform other programmers that this is a "read-only" method, and is guaranteed not mutate the object in any way. – therealrootuser Nov 17 '14 at 00:07
  • 1
    The `const` after `void hi()` applies to the implicit `A* this` (thus making it `const A* this`), not the return type. – Mike DeSimone Nov 17 '14 at 15:04
2
// **const object can call only const member function()** 

// **Modified Code**

#include <bits/stdc++.h>
using namespace std;

class A {
public:
    void hi() const {
        std::cout << "hi." << std::endl;
    }
};

class B {
public:
    void receive(const A& a) {
        a.hi();
    }
};

class C {
public:
    void receive(const A& a) {
        B b;
        b.receive(a);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    A a;
    C c;
    c.receive(a);

    return 0;
}
  • 1
    Encouraging the use of `#include ` in an answer on Stack Overflow is really asking for downvotes. [Why should I not #include ?](https://stackoverflow.com/q/31816095/10871073) – Adrian Mole Jun 22 '22 at 10:07
0
  1. As already mentioned, one option is to make hi method const-qualified.

  2. Another option is to use const_cast at the time of calling the hi method like so

A& ref = const_cast <A&>(a);
ref.hi();
monk
  • 39
  • 1