简体   繁体   English

main中未定义的方法,使用双端队列

[英]Method undefined in main, working with a deque

I am working with a deque 我正在与双端队列

MyDeque theQueue = new MyDeque(queElm);

in main MyDequeApp that calls a method 在主MyDequeApp中调用方法

theQueue = insertFront(10);

the method is in a class called MyDeque 该方法在名为MyDeque的类中

public void insertFront(Item x)
{
    if(front==maxSize)
        front=0;
    queArray[front++]=x;
    nItems++;
}

I am getting an error in main when calling insertFront saying 调用insertFront时我在main中遇到错误

The method insertFront() is undefined for type MyDequeApp 类型MyDequeApp的方法insertFront()未定义

I don't understand this error as I believe the method is defined in MyDeque , if someone could shed some light it would help me out. 我不明白此错误,因为我认为该方法是在MyDeque定义的,如果有人可以阐明一些方法,它将对我有所帮助。

You are passing int value, but it requires Item object. 您正在传递int值,但它需要Item对象。

 public void insertFront(Item x)

If you look at method signature, this method with name insertFront accepts values of type Item 如果查看方法签名,则此名称为insertFront方法insertFront接受Item类型的值

You're calling the method incorrectly. 您错误地调用了该方法。 insertFront is a method on the MyDeque class. insertFrontMyDeque类上的方法。 So to call it you need to call it from your instance of MyDeque . 因此,要调用它,您需要从MyDeque实例中调用它。 The error is saying you're calling it from MyDequeApp , not MyDeque . 错误是说您是从MyDequeApp而不是MyDeque调用它。

Replace: 更换:

theQueue = insertFront(10);

With: 带有:

theQueue.insertFront(10);

As Nambari mentioned, you're also passing the wrong kind of value. 正如Nambari所说,您还传递了错误的一种价值。 It's expecting an Item but you're passing an int . 它期待一个Item但您正在传递一个int Instead, create an instance of Item and pass that to the method. 而是创建Item的实例并将其传递给方法。

I'd personally also change the name of the variable since it's a deque, not a queue. 我个人也会更改变量的名称,因为它是双端队列,而不是队列。

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

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