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;
}
}
我得到這些錯誤試圖訪問一個std ::指數堆棧
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)]â
這些行
if(!stack[j]){//since j-1 when the index is 0 will cause an error
if(stack[j-1]==matchingBracket){
我擡頭的std ::棧和發現「默認情況下,如果沒有爲特定的堆棧類指定容器類,則使用標準容器類模板deque。」當我查找deque時,發現它支持operator []。這就是我宣佈我的籌碼。在此源文件的相應頭文件中。
#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
正如我所看到的,在std :: stack上使用operator []應該很好。有任何想法嗎?
從使用堆棧::℃;線我得到,錯誤:「堆」不是類或命名空間 –
@TylerPfaff重新加載頁面,我更新了錯誤修正 – Potatoswatter
這工作非常感謝,哪裏是一個很好的地方找到c + +參考? –