Fihop
Fihop

Reputation: 3177

c++ const error

    bool check_integrity( int pos ) const
    {
        if (( pos <= 0 ) || ( pos > max_seq ) || ( pos  >= _length + _beg_pos ))
        {
             cerr << "!! invalid position: " << pos
                  << " Cannot honor request\n";
             return false;
        }

        if ( _isa == ns_unset ) 
        {
             cerr << "!! object is not set to a sequence."
                  << " Please set_sequence() and try again!\n";
             return false;
        }

        if ( pos > _elem->size()){
             cout << "check_integrity: calculating "
                  << pos - _elem->size() << " additional elements\n";
             ( this->*_pmf )( pos );
        }

        return true;
    }


    public:
        typedef void (num_sequence::*PtrType)( int );
    private:
        PtrType    _pmf;

The above code clip is part of class "num_sequence". I got an error for the following line:

( this->*_pmf )( pos );

The error is: 'const num_sequence *const this' Error: the object has type qualifiers that are not compatible with the member function

Thanks!

Upvotes: 0

Views: 163

Answers (3)

AnT stands with Russia
AnT stands with Russia

Reputation: 320361

You are trying to call a non-const member function pointed by _pmf for a constant object *this. This is a violation of const-correctness rules.

Either declare your PtrType as

typedef void (num_sequence::*PtrType)( int ) const;

or remove const from your check_integrity function

bool check_integrity( int pos )
{
   ...

Either this or that. You didn't provide enough information for someone else to decide which is the right thing to do in this case.

Upvotes: 3

mmirzadeh
mmirzadeh

Reputation: 7079

You need to change

typedef void (num_sequence::*PtrType)( int );

to

typedef void (num_sequence::*PtrType)( int ) const;

since you are calling the function from a const function

Upvotes: 1

OJ.
OJ.

Reputation: 29401

check_integrity is a const function so functions it calls must also be const, hence to call a PtrType function is must also be const.

Try this:

typedef void (num_sequence::*PtrType)( int ) const;

NB: I didn't compile this :) Just thinking out loud.

Upvotes: 4

Related Questions