无法从“void”转换到“令牌” (C++)
编辑:将令牌结构/枚举添加到代码块
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
pop_front
不返回值。如果要删除第一个元素并读取其值,可以分两步完成:整个 STL 都采用此约定,主要是出于效率原因。通过让
front
返回值而pop_front
不返回任何内容,您可以根据需要捕获该值,但如果您只想删除该值,则无需创建只需调用 pop_front 即可删除已删除对象的不必要副本。稍后您将使用此代码遇到类似的错误:
要解决此问题,请将其分成两行:
希望这会有所帮助!
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:This convention is employed throughout the STL, mostly for efficiency reasons. By having
front
return the value andpop_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 callingpop_front
.You will run into a similar error later on with this code:
To fix this, split this into two lines:
Hope this helps!
它会给你错误,因为
std::list<>::pop_front()
是一个void
函数。它不返回任何内容。然而,你使用它就像它返回一些东西一样。所以,问题确实是针对您的:您为什么尝试使用void
函数作为返回值函数?Token t = tokenList.pop_front()
行是什么意思?如果您尝试从列表中“弹出”第一个元素,则可能的步骤顺序将包括
It gives you the error because
std::list<>::pop_front()
is avoid
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 avoid
function as a value-returning function? What do you mean byToken t = tokenList.pop_front()
line?If you were trying to "pop" the first element from the list, the possible sequence of steps would include