无法从类中的其他功能访问同一类的变量

Unable to access variable of same class from other functions inside the class

本文关键字:变量 一类 功能 其他 访问      更新时间:2023-10-16

我无法从类中的其他推送和弹出功能访问我的'arr'变量。在此范围中获取错误"'arr'并未声明"。应该可以在班上访问。我可以在静态上分配内存作为解决方案,但我想尝试一下。

该程序给出了反向抛光符号。
输入
1
(a b)
o/p
AB

#include <iostream>
#include <string>
using namespace std;

class stackOperations{
  private:
    int top = -1 ;
  public:
  void allocate(int len){
    char *arr = new char[len] ;
  }
  void deallocate(){
    delete []arr ;
  }
  void push(char x){
      top++;
      arr[top] = x ;
  }
  char pop()
  {
      char temp;
      temp = arr[top];
      top--;
      return temp;
  }
};
void RPN(string exp)
{
    stackOperations st ;
    int len = exp.length();
    st.allocate(len);
    for(int i=0; i <=len; i++){
        char tmp ;
        tmp = exp[i];
        if(tmp=='('){
            st.push('(');
        }
        else if(tmp==')'){
            char y = st.pop();
            while(y != '('){
                cout << y ;
                y = st.pop();
                }
            }
        else if(int(tmp) >= 97 && int(tmp) <= 122 ) {
            cout << tmp ;   
        }
        else {
            st.push(tmp);
        }
    }
    st.deallocate();
}
int main() {
    int test ;
    string exp ;
    cin >> test;
    cin.ignore();
    while(test!=0){
        getline(cin, exp);
        RPN(exp);
        cout << endl ;
        test--;
    }
    return 0;
}
class stackOperations{
    private:
          int top = -1 ;
          char *arr;
     public:
      void allocate(int len){
           arr = new char[len] ;
  }