Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

  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;
}
}

I'm getting these errors

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

for these lines

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

I looked up std::stack and found out that "By default, if no container class is specified for a particular stack class, the standard container class template deque is used." When I looked up deque I found out it supports operator[]. This is how I declared my stack. In the corresponding header file to this source file.

#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

As I see it, using operator[] on a std::stack should work just fine. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
155 views
Welcome To Ask or Share your Answers For Others

1 Answer

std::stack doesn't inherit from the underlying container type, it adapts it to a completely new interface. The underlying container is not exposed. That's essentially the point of the adaptors std::stack and std::queue: they ensure that you're using a more limited interface that will be the same regardless of the underlying structures.

That said, you can inherit from std::stack and access the underlying container from a subclass. It is a protected member named c.

class my_stack : public std::stack< char > {
public:
    using std::stack<char>::c; // expose the container
};

int main() {
    my_stack blah;
    blah.push( 'a' );
    blah.push( 'b' );
    std::cout << blah.c[ 1 ]; 
}

http://ideone.com/2LHlC7


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...