C# C-扩展堆栈类-错误

C# C-扩展堆栈类-错误,c#,C#,我得到一个错误:非泛型类型'System.Collections.Stack'不能与类型参数一起使用 // For use in BONUS section of lab: class LossyStack<T> : Stack<T> { private const int getAway=1; // Starts out at 1 (out of 20 random numbers - 5%) public Stack<string&g

我得到一个错误:非泛型类型'System.Collections.Stack'不能与类型参数一起使用

    // For use in BONUS section of lab:
    class LossyStack<T> : Stack<T> {
 private const int getAway=1;  // Starts out at 1 (out of 20 random numbers - 5%)
    public Stack<string> escsaped;

    public LossyStack(): base() {   // Constructor
        escaped = new Stack<string>();
    }

    public T Pop() {
  RandomNumber rand = new RandomNumber(1,20);  // One to twenty 

  if (rand<=getAway){
      escaped.push(base.Pop());
  }
  else {
        return base.Pop();
  }
  getAway++; // add another 1 to getAway so it increases by 5%
    }
  }

谁能告诉我怎么才能解决这个问题?非常感谢

嗯,也许你需要一个:

using System.Collections.Generic;
在您的.cs文件中进行更新。

替换:

使用系统集合


使用System.Collections.Generic

我看到的一个问题是,转义堆栈的类型是string,而您正从泛型堆栈的基类将其推入

另一个问题是getaway被声明为const,在这种情况下,您将无法在pop方法中增加它

在此处进行其他编辑: Pop方法不会为每个代码路径返回任何内容,因此它不会编译。您是否打算在推入转义堆栈时进行循环

我本打算把它清理干净,做些可行的事情,但我不确定这应该做什么。以下是一些代码,由于代码路径返回问题,它仍然无法编译,但已清理了一些:

class LossyStack<T> : Stack<T>
{
    private int getAway = 1;  // Starts out at 1 (out of 20 random numbers - 5%)
    public Stack<T> escaped = new Stack<T>();

    public LossyStack()
        : base()
    {   // Constructor
    }

    public T Pop()
    {
        Random rand = new Random();
        if (rand.Next(20) <= getAway)
        {
            escaped.Push(base.Pop());
        }
        else
        {
            return base.Pop();
        }
        getAway++; // add another 1 to getAway so it increases by 5%
    }
}

使用制度;使用System.Collections;//对于使用System.Collections.Generic的IEnumberable接口;使用System.Collections.Generic.Stack;仍然出现相同的错误。由于需要System.Collections,请更改定义,将:Stack更改为:System.Collections.Generic.Stack,并对本地成员和构造函数中的实例化执行相同的操作注意:您还需要将新关键字添加到pop方法以消除警告。堆栈虽然不是密封类,但不会将Pop或Push方法声明为虚拟方法,因此必须使用new隐藏函数。