无法从类中的其他功能访问同一类的变量
Unable to access variable of same class from other functions inside the class
我无法从类中的其他推送和弹出功能访问我的'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] ;
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- 如何为同一类对象的成员函数保留单独的变量副本?
- 无法从类中的其他功能访问同一类的变量
- C 将在类中创建的线程共享同一类变量
- 希望静态成员函数调用同一类的成员变量
- 如何在另一类设置静态变量
- C :从另一类调用类的变量
- 方法无法访问同一类的成员变量 (C++)
- 访问同一类的其他对象的变量
- C++继承:作为成员变量的同一类的两个子类
- 不允许直接在同一类中访问成员变量
- 通过同一类的私有成员访问私有成员变量
- 我无法使用类变量作为同一类函数的默认参数
- 在同一类的方法中使用类的静态成员变量
- 一个类的方法可以't访问同一类的int变量
- 如何在具有 2 个实现的同一类中保留静态变量的值
- 尝试返回同一类中的私有变量时C++ "Getter"方法抛出访问冲突