gpt4 book ai didi

c++ - 无法从类内的其他函数访问同一类的变量

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:10 24 4
gpt4 key购买 nike

我无法从类中的其他推送和弹出函数访问我的“arr”变量。收到错误“'arr'未在此范围内声明”。它应该可以在类里面访问。我可以静态分配内存作为解决方案,但我想试试这个。

该程序给出了反向波兰符号。
输入
1
(a+b)
啊/啊
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] ;
}

关于c++ - 无法从类内的其他函数访问同一类的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47955279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com