zptzdlzc

导航

'!‘绑定成员函数表达式上的非法操作(c++)

在练习栈的用法的时候,遇到了 '!‘绑定成员函数表达式上的非法操作 这个问题

源代码如下:

#include<iostream>
#include<stack>
using namespace std;
int main()
{
    stack<double>stac;
    for (double i = 0;i < 10.0;i++)
    {
        stac.push(i);
    }
    while (!stac.empty)
    {
        cout << stac.top() << endl;
        stac.pop();
    }
    cout << "栈的大小:" << stac.size() << endl;
    system("pause");
    return 0;
}

在网上查找后,发现了问题所在,即empty是一个函数,应该写成empty()

改完后的源代码:

#include<iostream>
#include<stack>
using namespace std;
int main()
{
    stack<double>stac;
    for (double i = 0;i < 10.0;i++)
    {
        stac.push(i);
    }
    while (!stac.empty())
    {
        cout << stac.top() << endl;
        stac.pop();
    }
    cout << "栈的大小:" << stac.size() << endl;
    system("pause");
    return 0;
}

posted on 2022-05-11 16:06  zdlzc  阅读(913)  评论(0编辑  收藏  举报