简体   繁体   English

错误:没有匹配功能可调用&#39;Stack <int> ::窥视()&#39;

[英]error: no matching function for call to 'Stack<int>::Peek()'

I keep getting the following error: error: no matching function for call to 'Stack::Peek()' 我不断收到以下错误:错误:没有匹配的函数来调用'Stack :: Peek()'

I am new to C++ and I cannot figure out why I am getting the error 我是C ++的新手,我无法弄清楚为什么出现此错误

This is my Peek() function. 这是我的Peek()函数。

int Peek(T data)
{
    if(IsEmpty ())
        return -1;
     else
        return top -> data;

}

and this is my main() function. 这是我的main()函数。

int main()
{
    Stack<int> s1;

    cout << "*declare stack s1\ns1=" << s1 << endl; // stack initially set to 0
    cout << "s1.Size()=" << s1.Size() << endl;
    cout << "s1.IsEmpty()=" << ((s1.IsEmpty()) ? "T" : "F") << endl;
    cout << "s1.IsFull()=" << ((s1.IsFull()) ? "T" : "F") << endl;
    cout << "s1.Peek()=" << s1.Peek() << endl;
    cout << endl;

    Stack<char> s4;

    for (char c='a'; c<='z'; c++)s4.Push(c);
    cout << "s4=" << s4 << endl;
    cout << "s4.Size()=" << s4.Size() << endl;
    cout << "s4.IsEmpty()=" << ((s4.IsEmpty()) ? "T" : "F") << endl;
    cout << "s4.IsFull()=" << ((s4.IsFull()) ? "T" : "F") << endl;
    cout << "s4.Peek()=" << s4.Peek() << endl;
}

I get the error whenever the Peek function is called in main so I was wondering if anyone could help me with this. 每当在main中调用Peek函数时,我都会收到错误消息,所以我想知道是否有人可以帮助我。

You defined a Peek method, but not Stack::Peek. 您定义了一个Peek方法,但没有定义Stack :: Peek。 Your method should have this signature: int Stack::Peek(T data) 您的方法应具有以下签名: int Stack::Peek(T data)

Because you are calling it without any argument. 因为您正在调用它而没有任何参数。 Your declaration: 您的声明:

int Peek(T data)

Your invocation: 您的调用:

s4.Peek()

Indeed a stack data type doesn't need an argument to the peek function (nor you use it). 实际上,堆栈数据类型不需要peek函数的参数(也无需使用)。 You should modify your original function to int Peek() . 您应该将原始函数修改为int Peek()

Your function Peak is declared as having one parameter 您的函数Peak被声明为具有一个参数

int Peek(T data);

However you call it without any argument 但是,您可以不加任何参数地调用它

cout << "s1.Peek()=" << s1.Peek() << endl;

So the compiler does not know a function with name Peak that has no parameter. 因此,编译器不知道名称为Peak的函数没有参数。

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

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