Próba uzyskania dostępu do indeksu std :: stack

  void PDA::parse(vector<string> words){
    for(int i=0; i<words.size();i++){//for each string in the input file
    string token=words[i];
    for(int j=0; j<token.length(); j++) //for each character in the string
      {
        char input=token[j];
        char matchingBracket=getMatchingBracket(input); //returns the matching bracket, should probably just have ( and [

        if(!stack[j]){//since j-1 when the index is 0 will cause an error
          if(stack[j-1]==matchingBracket){
            stack.pop();
          }else{
            stack.push(input);
          }

        }
  }
    accepted()?cout<<"The string "<<words[i]<<" is balanced and was accepted"<<endl : cout<<"The string "<<words[i]<<" is not balanced and was not accepted"<<endl;
}
}

Dostaję te błędy

PDA.cpp:25: error: no match for âoperator[]â in â((PDA*)this)->PDA::stack[j]â
PDA.cpp:26: error: no match for âoperator[]â in â((PDA*)this)->PDA::stack[(j - 1)]â

dla tych linii

if(!stack[j]){//since j-1 when the index is 0 will cause an error
              if(stack[j-1]==matchingBracket){

Sprawdziłem std :: stack i stwierdziłem, że „Domyślnie, jeśli nie określono klasy kontenera dla konkretnej klasy stosu, używany jest standardowy szablon deque klasy kontenera.” Kiedy spojrzałem na deque, dowiedziałem się, że obsługuje operatora []. W ten sposób zadeklarowałem swój stos. W odpowiednim pliku nagłówkowym do tego pliku źródłowego.

#ifndef PDA_H
#define PDA_H
#include <stack>
#include <vector>
#include <deque>
class PDA{
 private:
  std::stack<char> stack;
 public:
  PDA();
  bool isEmpty();
  void parse(std::vector<std::string>);
  char getMatchingBracket(char);
  bool accepted();
};
#endif

Jak widzę, użycie operatora [] na std :: stack powinno działać dobrze. Jakieś pomysły?

questionAnswers(3)

yourAnswerToTheQuestion