简体   繁体   中英

How can I pop something by reference?

I am doing a program to check for balanced brackets and parenthesis, etc. I created a char to store the info, and when I push the char it works, but it won't allow me to pop. Anyone know what I can do? Our prof gave us the header file so I cannot change it from int to char in the pop/push function. But I'm curious what I can do to make this work?

void push(int);
void pop(int &);

char ch,i;
IntStack x(50);
int count = 0;

while (fin>>ch)
{
if (ch == '[' || ch=='{' || ch=='(')
{
      x.push(ch);    //this works
  count++;
}

if (ch==']' || ch=='}' || ch==')')
{
  x.pop(ch);    //this brings an error, i also tried x.pop(ch&) and didnt work too
  count--;
}

}

You cannot bind a char to a non-const reference to int. Change the type of ch to int and you should be fine.

Pass an integer into pop that represents ch, then set ch according the results passed back from the integer.

int PopVar;
Pop(PopVar);
ch = PopVar;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM