Home > Software engineering >  Im having a code error, while doing a code for a circular link list
Im having a code error, while doing a code for a circular link list

Time:03-21

Im having a problem finding the solution on my code, it was running okay, but while I as editing the main to Run the code, something might have change. so now I'm having this error on my code that says: this' argument to member function 'isEmpty' has type 'const CircularLinkedList', but function is not marked const

This is the part of the code where the error is popping.

//Copy Constructor
 template <typename T>
 CircularLinkedList<T>::CircularLinkedList(const CircularLinkedList& c1)
{
    if (!c1.isEmpty())
{
    Node<T>* curr = c1.frst;

    //Iterating through elements until we encounter home node again
    while (curr->next != first)
    {
        insert(curr->data);
        curr = curr->next;
    }
  }
 }

The following code I didn't originally have it on my main-menu to run the code but once I put it on it the error pop up. I'm no sure if this has something to do with it. but as a reference here is the code that I added. I didn't have any error before this code.

int printMenu();

 // InsertList inserts an item into the list parameter
 void insertListItem ( CircularLinkedList<int> & );

 // deletes the first occurrence from the list parameter
void deleteOne ( CircularLinkedList<int> & );

// deletes all the occurrence from  list parameter
void deleteAll ( CircularLinkedList<int> & );

 //return the length of the list
 int totalCount( CircularLinkedList<int> & );

// searchItem searches for an item in the list parameter
void searchItem ( CircularLinkedList<int>  );

 // return the number of occurrences of a given item
 int totalOccurence (CircularLinkedList<int> & );

CodePudding user response:

The error is self-explanatory: in your CircularLinkedList<T>::CircularLinkedList copy constructor, the parameter is marked const.

You can only use its method that are defined as const. So change your isEmpty definition to something like

bool isEmpty() const {...}

After all, checking if the list is empty should not modify that list.

  • Related