Search code examples
c++linked-liststructurenodes

How to search a linked list for a structure object


for this program I am not allowed to use vectors or std::.

I have a program called StudentList.cpp that uses a linked list class and a structure that stores the students' info from a file. The removeStudent function asks the user to enter a student ID number. I build a temporary Student object and search the linked list for the id number. If the student is found then I display the student's entire record and remove the student.

This is where I am having problems. In the removeStudent function, I can't call the remove functions correcly. When I call the remove function from LinkedList.h I get the following error message:

error message: a reference type "Student &" (non const-quaified) cannot be initialized with value type "int"

Here are the functions I am having trouble with from the StudentList.cpp program:

void removeStudent(LinkedList<Student> &list)
{
    int id; 

    cout << "Please enter student ID number: ";
    cin >> id;

    Student temp;    //structure Student temporary object

    if (list.remove(temp.id) == false)      //error message: a reference type "Student &" (non const-quaified) cannot be initialized with value type "int"
        cout << "Student not found.\n";
    else
    {
        cout << id << endl;
        cout << "Student removed.\n";
    }
}

I have a program called LinkedList.h that includes functions working with a linked list. I know this program works just fine. The function I am calling from removeStudent is:

template <class TYPE>
bool LinkedList<TYPE>::remove(TYPE &dataOut)
{
    bool success = false;
    Node<TYPE> *pTemp = front;
    Node<TYPE> *pPrev = nullptr;

    while (pTemp != nullptr && pTemp->data < dataOut)
    {
        pPrev = pTemp;
        pTemp = pTemp->next;
    }
    if (pTemp != nullptr && pTemp->data == dataOut)
    {
        dataOut = pTemp->data;
        if (pPrev != nullptr)
            pPrev->next = pTemp->next;
        else
            front = pTemp->next;

        delete pTemp;
        success = true;
    }

    return success;

}

Solution

  • Thank you for the update. You are calling a function which expects one data type, but passing it a different datatype. The compiler correctly rejects this because you gave it the wrong data type.

    template<class TYPE>
    class LinkedList {
    public:
        bool remove(TYPE& x);
    };
    
    template <class TYPE>
    bool LinkedList<TYPE>::remove(TYPE& dataOut)
    {
        return false;
    }
    
    struct Student { int id; };
    
    int main() {
        LinkedList<Student> list;
    
        int id = 1;
        list.remove(id); // reproduced error here
    
        Student temp{ id };
        list.remove(temp); // correct here
    }
    

    That code gives this error:

    error C2664: 'bool LinkedList::remove(TYPE &)': cannot convert argument 1 from 'int' to 'TYPE &'

    The code compiles and runs if I comment out the bad line. Note that the next lines call list.remove with an instance of Student.

    The exact text of the error will differ by compiler, but essentially you passed it an integer, but it expected a reference to a Student object.

    (also you didn't initialize that variable, but that's a completely different issue)

    Since the class it a template class, when you look at the prototype "bool LinkedList::remove(TYPE& dataOut)" you know that it has to be of some type that is not specified in the template class. It is actually decided here:

    LinkedList<Student> list;  
               ^^^^^^^
    

    TYPE is Student, but it doesn't matter that it was a template. That just made it a tiny bit harder to read the header and know what type the function expects.