c++ 对堆栈的引用不明确[已关闭]

6ljaweal  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(250)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
2天前关闭。
Improve this question
我们的教授要求我们写一个程序,把中缀转换成后缀,要有步骤。我正在写程序,但当我想测试哪个运算符更高时,问题就开始了。
此功能:

int higherOP(char top, char op) //top, operator
{
    int top_Weight = OPweight(top);
    int op_Weight = OPweight(op);
    
    if(top_Weight > op_Weight) //top > OP2
    {
        //push top
        stack.push(top); //!!! ERROR: missing template arguments before '.' token 
        //but when I put stack global, another error happend !!! ERROR: reference to stack is ambiguous  ?????? 
        cout<<top<<" is pushed to the stack.";
    }
    else if(top_Weight < op_Weight)  //top < OP2
    {
        //pop top
        stack.pop(top); 
        cout<<top<<" has been poped.";
    }
    else  //top = OP2
    {
        //pop top
        stack.pop(top); 
        cout<<top<<" has been poped."; 
    }
}

这是完整的代码,(它还没有完成,因为我不确定如何解决错误...)

#include <iostream>
#include <string>
#include <stack> //stack libary!!!
using namespace std;

int OPweight(char OP) //OP=Operator  
{
    switch(OP)  
    {
    case '+':
    case '-':
        return 1;
    
    case '*':
    case '/':
        return 2;  
    }
}

int higherOP(char top, char op) //top, operator
{
    int top_Weight = OPweight(top);
    int op_Weight = OPweight(op);
    
    if(top_Weight > op_Weight) //top > OP2
    {
        //push top
        stack.push(top); //!!! ERROR: missing template arguments before '.' token 
        //but when I put stack global, another error happend !!! ERROR: reference to stack is ambiguous  ?????? 
        cout<<top<<" is pushed to the stack.";
    }
    else if(top_Weight < op_Weight)  //top < OP2
    {
        //pop top
        stack.pop(top); 
        cout<<top<<" has been poped.";
    }
    else  //top = OP2
    {
        //pop top
        stack.pop(top); 
        cout<<top<<" has been poped."; 
    }
}

bool isNumber(char Num)
{
    if(Num >= '0' && Num <= '9') return true; //take numbers (one digit only)
    if(Num >= 'a' && Num <= 'z') return true; //take letters (lowercase)
    if(Num >= 'A' && Num <= 'Z') return true; //take letters (uppercase)  
    return false; 
}

bool isOperator(char OP) 
{
        if(OP == '+' || OP == '-' || OP == '*' || OP == '/')
        return true;
        
    return false; 
}

////////

string toPostfix(string infix)
{
  cout<<"Expression: "<<infix<<" has been received.\n"; 
  
  stack<char> stack; //stack 
  string postfix = "";
  
  for(int i=0; i< infix.length(); i++) //loop through the infix, while looping, we will either FIND a number or an operator, and ( or ) ...
  {
    if(isNumber(infix[i])) // number, put in postfix string
    {
        postfix += infix[i]; cout<<infix[i]<<" added to the postfix string.\n";
    }
    
    else if(isOperator(infix[i])) // operator, push into stack, know weight, and act accordinly
    {
        //if the stack is empty, push the operator || if not empty and not (, know weight, and act accordinly
        while(!stack.empty() && stack.top()!='(' && higherOP(S.top(),infix[i]) ) 
        {
              postfix += stack.top(); 
              stack.pop(); cout<<infix[i]<<" has been poped outside the stack.\n";
        }
        stack.push(infix[i]); cout<<infix[i]<<" has been pushed inside the stack.\n"; 
    }
    
    else if(infix[i]=='(') // ( , push into stack, and ignore the operators below
    {
    
    }
    
    else if(infix[i]==')') // ) ,pop everything until you find (
    {
    
    }
    
  }
  
}

//////////////////

int main()
{
    string infix;
    cout<<"Infix to postfix program \n";
    cout<<"Please enter the infix exprission: ";
    cin>>infix;
    
    toPostfix(infix);
    
}
ljsrvy3e

ljsrvy3e1#

有两个因素在很大程度上造成了混乱:
1.您使用using namespace std;,从而将数千个非常常见的名称带入全局名称空间,包括std::stack(现在可以称为stack)。
1.您用与类完全相同的名称命名变量。这通常是令人困惑的,你可以期待许多奇怪的错误消息,这些消息似乎与你所写的无关。
结合这两个令人困惑的因素,你会得到一个像你一样的错误。在higherOP中,您尝试从toPostfix访问stack变量,但higherOP无法访问toPostfix中的局部变量,因此无法访问它。但是,它可以访问std::stack,不幸的是,您使用using namespace std;将其带入了全局命名空间。因为这是一个类模板而不是一个变量,所以operator .对这个名字没有任何意义。
当你把stack变量移到全局作用域时,它是不明确的,因为编译器不能分辨你是指类模板std::stack还是全局变量stack
我强烈建议避免使用using namespace std;(因为它通常是a bad idea),并避免将变量命名为与它们是其示例的类完全相同。然后你会得到一个更好的错误消息,比如'myStack' was not declared in this scope,它更容易理解和解决。

相关问题