2016-10-16 5 views
-1

私はこのエラーが発生しており、何をすべきか分かりません。 リンクされたリストがあり、セグメントを機能させようとしています。エラー:(const A)as this(this)

class Set() 
{ 
public: 
    struct Node 
    { 
     int value; 
     Node *next; 
     Node(int e, Node *n) : value(e),next(n){}; 
    }; 
    enum Exceptions{EMPTYSET, FULLMEM, REPLICA}; 

    Set() : _head(NULL){} 
    Set(const Set& s); 
    Set& operator=(const Set& s); 
    ~Set(); 

    void pop(int e); 
    void ext(int e); 
    bool contains(int e); 
    bool empty() const {return _head==NULL;} 

    friend Set unio    (const Set& a, const Set& b); 
    friend Set segment   (const Set& a, const Set& b); 
    friend std::ostream& operator<< (std::ostream& s, Set& a); 


private: 
    Node *_head; 
} 

私が試したことはこれです:

Set segment(const Set& a,const Set& b) 
{ 
Set c; 
Set::Node *p = a._head; 
while(p!=NULL){ 
    if(b.contains(p->value)){ 
     c.ext(p->value); 
    } 
    p=p->next; 
} 
return c; 
} 

EXT機能がセットに要素を置く:

void Set::ext(int e) 
{ 
try 
{ 
    Node *p = _head; 
    Node *pe = NULL; 
    Node *q = new Node(e,NULL); 
    while(p!=NULL && p->value < e) 
    { 
     pe = p; 
     p = p->next; 
    } 

    if(pe==NULL) 
    { 
     q->next =_head; 
     _head = q; 
    }else 
    { 
     q->next = p; 
     pe->next = q; 
    } 
}catch (std::bad_alloc o) 
{ 
    throw FULLMEM; 
} 
} 

そしてがあれば関数はtrueを返します要素がセットに含まれています。

bool Set::contains(int e) 
{ 
Node *p = _head; 
while(p!=NULL){ 
    if(p->value == e) 
     return true; 
    p=p->next; 
} 
return false; 
} 

、全体のエラーメッセージは次のとおりです。

||In function 'Set segment(const Set&, const Set&)':| 
error: passing 'const Set' as 'this' argument of 'bool Set::contains(int)' discards qualifiers [-fpermissive]| 

答えて

0

あなたはconst関数としてcontainsを宣言することができます:

class Set { 
    ... 
    bool contains(int e) const; 
    ... 
} 

bool Set::contains(int e) const { 
    ... 
} 

あなたはそれでいる間、あなたはおそらく同じことを行う必要がありますempty()へ。

いくつかの背景の読書のために、Meaning of "const" last in a C++ method declaration?

+0

はどうもありがとうございまし参照してください!すべては今働いている、私は確かにそれを読むつもりです! – Bako

関連する問題