无法从“void”转换到“令牌” (C++)

发布于 2024-11-26 02:40:15 字数 1108 浏览 1 评论 0原文

编辑:将令牌结构/枚举添加到代码块

我是 C++ 新手,所以如果我错过了一些明显的东西,请原谅我。我正在尝试编写 C++ 版本的调车场算法,但它无法编译,因为它给出了错误:“无法从 'void' 转换为 'Token'(在我标记的行上)。”谁能告诉我为什么会出现这个错误?

typedef enum TokenType { None, Number, Operator, LeftParens, RightParens };

struct Token
{
    enum TokenType type;
    union
    {
        int num;
        char op;
    };
};

list<Token> DoShuntingYard(list<Token> tokenList)
{
    stack<Token> opStack;
    list<Token> output;
    while (!tokenList.empty())
    {
        ****(This Line) Token t = tokenList.pop_front();
        switch (t.type)
        {
        case Number:
            output.push_back(t);
            break;
        case Operator:
            if (!opStack.empty())
            {
                Token op2 = opStack.top();
                if ((IsLeftAssoc(t) && GetOpPrecedence(t) <= GetOpPrecedence(op2)) || (!IsLeftAssoc(t) && GetOpPrecedence(t) < GetOpPrecedence(op2)))
                {
                    output.push_back(opStack.pop());
                }
            }
            break;
        }
    }
}

Edit: Added token struct/enum to code block

I'm new to c++, so forgive me if I missed something obvious. I'm trying to write a c++ version of the Shunting Yard Algorithm, but it won't compile because it gives me the error: "cannot convert from 'void' to 'Token' (on the line I marked)." Can anyone tell me why it gives this error?

typedef enum TokenType { None, Number, Operator, LeftParens, RightParens };

struct Token
{
    enum TokenType type;
    union
    {
        int num;
        char op;
    };
};

list<Token> DoShuntingYard(list<Token> tokenList)
{
    stack<Token> opStack;
    list<Token> output;
    while (!tokenList.empty())
    {
        ****(This Line) Token t = tokenList.pop_front();
        switch (t.type)
        {
        case Number:
            output.push_back(t);
            break;
        case Operator:
            if (!opStack.empty())
            {
                Token op2 = opStack.top();
                if ((IsLeftAssoc(t) && GetOpPrecedence(t) <= GetOpPrecedence(op2)) || (!IsLeftAssoc(t) && GetOpPrecedence(t) < GetOpPrecedence(op2)))
                {
                    output.push_back(opStack.pop());
                }
            }
            break;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倥絔 2024-12-03 02:40:15

问题是 pop_front 不返回值。如果要删除第一个元素并读取其值,可以分两步完成:

Token t = tokenList.front();
tokenList.pop_front();

整个 STL 都采用此约定,主要是出于效率原因。通过让 front 返回值而 pop_front 不返回任何内容,您可以根据需要捕获该值,但如果您只想删除该值,则无需创建只需调用 pop_front 即可删除已删除对象的不必要副本。

稍后您将使用此代码遇到类似的错误:

output.push_back(opStack.pop());

要解决此问题,请将其分成两行:

output.push_back(opStack.top());
opStack.pop();

希望这会有所帮助!

The problem is that pop_front does not return a value. If you want to remove the first element and read its value, you can do so in two steps:

Token t = tokenList.front();
tokenList.pop_front();

This convention is employed throughout the STL, mostly for efficiency reasons. By having front return the value and pop_front return nothing, you can capture the value if you want, but if you just want to remove the value you can do so without making an unnecessary copy of the removed object by just calling pop_front.

You will run into a similar error later on with this code:

output.push_back(opStack.pop());

To fix this, split this into two lines:

output.push_back(opStack.top());
opStack.pop();

Hope this helps!

静水深流 2024-12-03 02:40:15

它会给你错误,因为 std::list<>::pop_front() 是一个 void 函数。它不返回任何内容。然而,你使用它就像它返回一些东西一样。所以,问题确实是针对您的:您为什么尝试使用 void 函数作为返回值函数? Token t = tokenList.pop_front() 行是什么意思?

如果您尝试从列表中“弹出”第一个元素,则可能的步骤顺序将包括

Token t = tokenList.front();
tokenList.pop_front();

It gives you the error because std::list<>::pop_front() is a void function. It doesn't return anything. Yet, you are using it as if it returns something. So, the question is really to you: why are you attempting to use a void function as a value-returning function? What do you mean by Token t = tokenList.pop_front() line?

If you were trying to "pop" the first element from the list, the possible sequence of steps would include

Token t = tokenList.front();
tokenList.pop_front();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文