-1

I am getting this error and I have no idea what to do. So I have a linked list and I am trying to make the segment function.

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;
}

What I've tried is this:

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;
}

The ext function puts an element into the set:

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;
}
}

And the contains function returns true if an element is in the set.

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

And the whole error message is :

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

1 Answers1

0

You could declare contains as a const function:

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

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

While you're at it, you should probably do the same to empty().

For some background reading, see Meaning of "const" last in a C++ method declaration?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012