2012-08-10 35 views
0
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:C++ const的錯誤

(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

謝謝!

回答

3

您正在試圖調用由指向非const成員函數_pmf對於恆定對象*this。這違反了const-correctness規則。

無論是宣佈你PtrType作爲

typedef void (num_sequence::*PtrType)(int) const; 
check_integrity功能

bool check_integrity(int pos) 
{ 
    ... 

非此即彼

或刪除const。您沒有提供足夠的信息讓其他人決定在這種情況下應該做哪些事情。

4

check_integrityconst功能,所以功能調用也必須const,因此調用PtrType功能也必須是const

試試這個:

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

注:我沒編譯這個:)只是想大聲。

1

您需要更改

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

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

,因爲你是從const函數調用函數

相關問題