C++ 我的代码中出现了这样一个错误:void表达式的使用无效
在函数中,函数向量In_to_post(向量中缀)(向量=(1+2)*3^4)正在转换为形式为(后缀:12+3 4^*)的输出向量,我收到一个错误,表示无效表达式的使用无效。我已经对我得到这个错误的行进行了注释C++ 我的代码中出现了这样一个错误:void表达式的使用无效,c++,c++11,C++,C++11,在函数中,函数向量In_to_post(向量中缀)(向量=(1+2)*3^4)正在转换为形式为(后缀:12+3 4^*)的输出向量,我收到一个错误,表示无效表达式的使用无效。我已经对我得到这个错误的行进行了注释 char get_optr(Token t) { // your code here return t.optr; } vector<Token> in_to_post(vector<Token> infix)
char get_optr(Token t) {
// your code here
return t.optr;
}
vector<Token> in_to_post(vector<Token> infix)
{
vector<Token> output;
// your code here
stack<Token>b;
for(int i=0;i<infix.size();i++)
{
if(is_opnd(infix[i]))
{
output.push_back (infix[i]);
}
else if(get_optr(infix[i])=='(')
{
b.push(infix[i]);
}
else if(get_optr(infix[i])==')')
{
while(b.empty()==false)
{
//char i=get_optr(b.pop());
if(get_optr(b.pop())=='(') // I am getting error here
{
break;
}
else
{
output.push_back(b.pop()); //error
}
}
}
else if(is_opnd(infix[i])==false)
{
// char i=infix[i];
//char j=b.top();
if((priority(infix[i])>priority(b.top()))||get_optr(infix[i])=='('||get_optr(infix[i])==')')
{
b.push(infix[i]);
}
}
}
while(b.empty()==false)
{
output.push_back(b.pop()); //error
}
return output;
}
char get_optr(令牌t){
//你的代码在这里
返回t.optr;
}
_to_post中的向量(向量中缀)
{
矢量输出;
//你的代码在这里
斯塔克B;
对于(int i=0;i优先级(b.top())| | get_optr(中缀[i])='('| | get_optr(中缀[i])=')
{
b、 推(中缀[i]);
}
}
}
while(b.empty()==false)
{
output.push_back(b.pop());//错误
}
返回输出;
}
问题在于,在std::stack
上,只需将顶部元素从堆栈中弹出,但不返回它。此函数有一个void
返回类型,如果您试图在需要值的上下文中使用它,则会出现错误
output.push_back(b.pop()); //error
相反,您可以使用获取std::stack
output.push_back(b.top()); // ok
注意,这将简单地返回对top元素的引用,而不会弹出它。您可以在
top
之后调用pop
来弹出它。对于这个问题非常有用。如何定义Token
?Token
是否别名为void
或void*
?Token被定义为adtstruct令牌{//your code here int optnd;char optr;bool isopond;};给出触发错误的行将非常有用。非常感谢@cigien,现在是代码works@Tom没问题。如果回答了你的问题,请考虑接受答案。