error: pass "const A" together "this" dispute of "void A::hi()" discardsqualifiers <-fpermissive>

I don"t understand why I"m gaining this error, I"m no returning anything just passing the recommendation of the object and that is it.

You are watching: Passing const as this argument discards qualifiers

#include class A{public: void hi() { std::cout

I addressed it utilizing const correctness yet now I"m do the efforts to call methods within of the same technique and I gain the very same error, however the weird thing is the I"m no passing the referral to this method.

#include course A{public: void sayhi() const hello(); world(); void hello() { std::cout error: pass "const A" as "this" argument of "void A::hello()"discards qualifiers <-fpermissive>

error: happen "const A" together "this" dispute of "void A::world()"discards qualifiers <-fpermissive>



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

You can read much more about continuous member features here and correct usage of the const keyword here.



As already mentioned, one option is to do hi an approach const-qualified.

Another option is to usage const_cast at the moment of call the hi technique like so

A& ref = const_cast (a);ref.hi();


many thanks for contributing response to ridge Overflow!

Please be sure to answer the question. Provide details and also share her research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; earlier them increase with recommendations or an individual experience.

See more: To Move On Is To Grow

To learn more, see our tips on writing good answers.

article Your answer Discard

By clicking “Post your Answer”, you agree come our regards to service, privacy policy and also cookie policy

error: happen 'const …' as 'this' argument of '…' discards qualifiers in phone call method
an odd C++ error: test.cpp:15: error: pass ‘const *’ together ‘this’ debate of ‘*’ discards qualifiers
error: passing 'const …' as 'this' discussion of '…' discards qualifiers in calling technique
C++ Error - happen 'const Dataset' together 'this' argument discards qualifiers<-fpermissive>
site design / logo design © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.11.25.40831

her privacy

By clicking “Accept all cookies”, you agree ridge Exchange can store cookie on your maker and disclose info in accordance with our Cookie Policy.