1

I have to make a list using OOP in c++. It is supposed to have functions add, append, display, find, clear and delete.

I have succeeded with all functions except delete. The error is

for prev->getnext()* :  "Lvalue required as left operand of assignment"

The classes:

class student
    {
        private:
            string name;
            string faculty;
            student *next;
            
        public:
            student()
            {
                next = NULL;
            }
            
            student(string n, string f)
            {
                next = NULL;
                name = n;
                faculty = f;
            }
            
            void setname(string n)
            {
                name = n;
            }
            
            void setfac(string f)
            {
                faculty = f;
            }
            
            void setnext(student* stn)
            {
                next = stn;
            }
            
            student*getnext()
            {
                return next;
            }
            
            string getname()
            {
                return name;
            }
            
            string getfac()
            {
                return faculty;
            }
    };
    
    class studentlist
    {
        
        student *head;
            
        public:
            studentlist()
            {
                head = NULL;
            }
            
            void addstudent(string n, string f)
            {
                student *newstud = new student;
                newstud->setname(n);
                newstud->setfac(f);
                newstud->setnext(head);
                head = newstud;
            }
            void del(string n, string f);
            
            student* gethead()
            {
                return head;
            }
    };
    

and here is the function

    void studentlist::del(string n, string f)
    {
        student *current;
        student *aft;
        student *prev;
        
        current = head;
        
        while((current != NULL) && (current->getname() == n and current->getfac() == f))
        {
            prev = current;
            current = current->getnext();
            if(current = NULL)
                return;
            
            if(current->getname() == n and current->getfac() == f)
            {
                prev = current;
                prev->getnext() = current->getnext(); >here is the problem
                delete current;
            }

I don't understand how prev does not have any value.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 4
    `prev->getnext()` returns a temporary pointer (a prvalue in C++ nomenclature), but you're trying to assign a value to that expression. In C++'s eyes, this is like writing `a + b = 2`; it doesn't make sense to try assigning a value to a temporary. You probably want to use your `student::setnext` member function. – Brian61354270 Dec 02 '21 at 22:04
  • 1
    For more on lvalues vs (p)rvalues, see [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/q/3601602/11082165) and [expression value categories](https://en.cppreference.com/w/cpp/language/value_category). – Brian61354270 Dec 02 '21 at 22:05
  • On a separate note, `if(current = NULL)` is performing an assignment, not a comparison. You need `==` instead. Your compiler should have warned you about this. – Remy Lebeau Dec 02 '21 at 23:40

0 Answers0