Domanda

I have a function that looks like

bigvalue_t do_bigadd (const bigvalue_t& left, const bigvalue_t& right) {

}

and it is being called here

bigint bigint::operator+ (const bigint& that) const {
    bigint result;
    result.big_value = do_bigadd(this->big_value, that.big_value);
}

I'm getting the following compilation error

error: passing ‘const bigint’ as ‘this’ argument of ‘bigvalue_t bigint::do_bigadd(const bigvalue_t&, const bigvalue_t&)’ discards qualifiers [-fpermissive]

I know what is wrong, but I can't think of a way to deal with it. How do I make 'this' as const bigint& when it is a pointer?

È stato utile?

Soluzione

Your problem is that do_bigadd is not const (but this is const in operator+, and so you are trying to call a non-const member function of the const object this). You need to make do_bigadd const:

bigvalue_t do_bigadd (const bigvalue_t& left, const bigvalue_t& right) const {
}

If this in turn causes any issues in do_bigadd, you'll have to iron them out. However, based on the fact that you are passing both operands to do_bigadd, I'm wondering if you actually meant for it to be a static or non-member function in the first place.

Altri suggerimenti

You say you have a function:

bigvalue_t do_bigadd (const bigvalue_t& left, const bigvalue_t& right) {

}

That is not true. You have a member function:

bigvalue_t bigint::do_bigadd (const bigvalue_t& left, const bigvalue_t& right) {

}

and you need to make that function either:

  • a free (non-member) function
  • a static function
  • mark it as const (as in Jason C's answer)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top