0

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?

yizzlez
  • 8,757
  • 4
  • 29
  • 44

2 Answers2

5

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.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Is it not permissible to pass the data of a const object to an non-const member function? I am confused why can't I just pass big_value of 'this' without declaring 'this' as const in do_bigadd. – user3345589 Apr 24 '14 at 00:45
  • 1
    @user3345589 No, it's because you've actually declared `do_bigadd` as a member function (looks like you've done it inline). You don't show this in context but that's what the error message implies. So you have `bigint::do_bigadd`, and its not const. Then you have `bigint::operator+`, which *is* const. Therefore, in the context of `operator+`, `this` is also `const` (because `operator+` itself is `const`). Then is you basically call `this->do_bigadd(...)`, but you can't, because `this` is const in that context and `do_bigadd` is not. It isn't actually related to the parameter types. – Jason C Apr 24 '14 at 00:55
4

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)
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • how do i make it a free function? they are the source that my professor provided, i dont plan to change them, he told us to add that function in the private section of class bigint – user3345589 Apr 23 '14 at 23:54
  • @user3345589 If it's in the class, it is automatically either a non-static or a static member function, but by definition it can't be a free function. If you want a free function, move it out of the class. In fact, the function does not have to do anything with the class itself, so it should not be a part of it. – Daniel Frey Apr 24 '14 at 00:02
  • @Frey That is what I am thinking, this function should not even be in private functions of bigint, since it has nothing to do with it. The functions that the professor told us to add don't compile by itself, I think it is the way the professor tries to trick us to learn? I am new to c++, I am not sure what the const does after the function parameters, can you elaborate more on that? – user3345589 Apr 24 '14 at 00:23
  • @user3345589 The `const` can only be added to member functions, not to free functions. See [**this Q/A**](http://stackoverflow.com/questions/751681). – Daniel Frey Apr 24 '14 at 00:43