-2

I am using Doubly Linked List Data Structure of CPP and have written the following code

#include<iostream>
#include <bits/stdc++.h> 
using namespace std;


int main()
{
        
    
    list <int> l1[64]; 
    l1[1].push_back(100);
    l1[2].push_back(200);
    int p=l1[1].pop_front();
    cout<<p<<endl;
     p=l1[2].pop_front();
    cout<<p<<endl;
    

}

But I am getting this errors-->

trial.cpp: In function ‘int main()’:
trial.cpp:15:23: error: void value not ignored as it ought to be
   15 |  int p=l1[1].pop_front();
      |        ~~~~~~~~~~~~~~~^~
trial.cpp:17:20: error: void value not ignored as it ought to be
   17 |   p=l1[2].pop_front();
      |     ~~~~~~~~~~~~~~~^~

I am unable to understand the reason for this error, because I am just popping the first element into the p variable and printing it. Then why am I getting this error?

  • 3
    https://en.cppreference.com/w/cpp/container/list/pop_front Doesn't need a long read to spot the problem in your code. – πάντα ῥεῖ Nov 21 '20 at 07:42
  • 1
    @CODEGEEK Well not me, but I guess the problem is the lack of research effort. If you had looked it up you would have seen that `pop_front` does not return anything, and even if you hadn't looked it up the compiler is telling you the same thing. – john Nov 21 '20 at 07:43
  • 2
    see also [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Nov 21 '20 at 07:45
  • the ```pop_front()``` does not return any value and that's what the compiler is telling you. You cannot assign ```pop_front()``` to a variable because it is a void function. – iwrestledthebeartwice Nov 21 '20 at 07:47
  • You're getting this error because you guessed at what `pop_front()` does instead of reading the documentation. Even if you make a habit if guessing, once your guess is shown to be wrong, your next stop should be to look up what the function actually does. – Pete Becker Nov 21 '20 at 13:14

1 Answers1

1

pop_front does not return anything. The reason is that it would be impossible to implement the strong exception safety guarantee if it did. To access the front element of your list use front. Here's your code rewritten.

int main()
{    
    list <int> l1[64]; 
    l1[1].push_back(100);
    l1[2].push_back(200);
    int p=l1[1].front();
    l1[1].pop_front();
    cout<<p<<endl;
    p=l1[2].front();
    l1[2].pop_front();
    cout<<p<<endl;
}
john
  • 85,011
  • 4
  • 57
  • 81