【问题标题】:popping a stack out of a stack/queue从堆栈/队列中弹出堆栈
【发布时间】:2018-11-01 21:52:57
【问题描述】:

我的问题是关于如何从队列中取出堆栈。该程序应该通过生成堆栈(如下所示),将这些堆栈塞满数据(如下所示),然后卸载并显示其中的数据来工作。现在它只是向我抛出一个 CS1061 异常。例如,5 就在那里,实际代码是从数组中选择一个随机字符串。

    public void newCustomers()
    {
        var customer = new Stack();
        store.Enqueue(customer);
    }
    public void Shop()
    {
        var customer = store.Dequeue();
        customer.Push(5);
        //^currently this doesn't work. I'm assuming the typing for customer is wrong.
        store.Enqueue(customer);
    }

CS1061

严重性代码描述项目文件行抑制状态 错误 CS1061 'object' 不包含 'Push' 的定义,并且找不到接受“object”类型的第一个参数的可访问扩展方法“Push”(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

  • 我不知道什么是 CS1061。
  • 请编辑您的问题并粘贴错误信息。
  • 您是否缺少new Stack() 的类型?不应该是new Stack<int>()吗?

标签: c# stack queue


【解决方案1】:

您正在使用非泛型 Queue 类。 Dequeue() 方法返回一个 object,您必须将其转换为 Stack

var customer = (Stack)store.Dequeue();
customer.Push(5);

我建议改用通用队列类Queue<T>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-17
    • 2014-04-21
    • 2013-09-18
    • 2018-05-09
    • 2012-10-14
    • 2017-06-01
    • 1970-01-01
    • 2018-04-12
    相关资源
    最近更新 更多