简体   繁体   English

空值不会被忽略,因为它应该是错误

[英]Void value not ignored as it ought to be Error

I am using Doubly Linked List Data Structure of CPP and have written the following code我正在使用 CPP 的双向链表数据结构并编写了以下代码

#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.我无法理解此错误的原因,因为我只是将第一个元素弹出到p变量中并打印出来。 Then why am I getting this error?那为什么我会收到这个错误?

pop_front does not return anything. pop_front不返回任何内容。 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 .要访问列表的前端元素,请使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM