2016-04-06 5 views
0

Ich mache ein Klammern-Matching-Programm für meine Hausaufgaben und ich bin nicht ganz sicher, wie genau das zu tun ist. Ich erhalte Fehler, wenn ich meine Stack-Funktionen anrufe, die besagen, dass ein nicht statischer Memberverweis mit einem relativen Objekt in der is_balanced-Funktion erstellt werden muss. Warum tritt dieser Fehler auf und wie kann ich ihn beheben? Danke für die Hilfe.Klammern entsprechen Programm mit Stacks (nicht STL-Template-Stacks)

#include <iostream> 

#include <string> 

#include "StringStack.h" 


using namespace std; 

char parentest[]; 

int main() { 

    //variables 

    string paren; 

    StringStack *stack; 

    //user input 

    cout << "Please enter a string of parentheses"; 

    cin >> paren; 

    //turn string into char array 

    char parentest[paren.length] = paren; 

    //set size of the stack equal to the length of the char array. 

    stack->stackSize = parentest.length; 

    //display the string of balanced parantheses 

    cout << is_balanced(parentest); 

    system("Pause"); 

    return 0; 

} 

//balances the parentheses and returns the string 

bool is_balanced(string s) { 
    bool status; 
    for (int i = 0; i < s.length, i++) { 

     cout << s[i] << endl; 

    StringStack::push(s[i]); 
    } 
    StringStack::isEmpty(); 
    if (StringStack::isEmpty) { 
     status = true; 
    } 
    return status; 
} 

//constructor creates an empty stack 

StringStack::StringStack(int size) 

{ 

    stackArray = new string[size]; 

    stackSize = size; 

    top = -1; 

} 

//copy constructor 

StringStack::StringStack(const StringStack &obj) { 

    //creates stack array 

    if (obj.stackSize > 0) { 

     stackArray = new string[obj.stackSize]; 

    } 

    else { 

     stackArray = nullptr; 

    } 

    //copies stack contents 

    stackSize = obj.stackSize; 

    for (int count = 0; count < stackSize; count++) { 

     stackArray[count] = obj.stackArray[count]; 

    } 

    //set top of stack 

    top = obj.top; 

} 



//Destructor 

StringStack::~StringStack() { 

    delete[] stackArray; 

} 

//puts a value at top of the stack 

void StringStack::push(string p) { 

    if (isFull()) { 

     cout << "The stack is full. \n"; 

    } 

    else { 

     top++; 

     stackArray[top] = p; 

    } 



} 

void StringStack::pop() { 

    if (isEmpty()) { 

     cout << "the stack is empty"; 

    } 

    else { 

     top--; 

     return top; 

    } 



} 

bool StringStack::isFull() const { 



    bool status; 



    if (top == stackSize - 1) { 

     status = true; 

    } 

    else { 

     status = false; 

    } 

    return status; 

} 

bool StringStack::isEmpty() const { 

    bool status; 



    if (top == -1) { 

     status = true; 

    } 

    else { 

     status = false; 

    } 

    return status; 

} 

Antwort

0

Weil Sie nicht statische Elementfunktionen aufrufen, als wären sie statisch. Sie weisen auch keine Instanz von StringStack zu, obwohl Sie einen StringStack-Zeiger haben. Wenn Sie diesen Zeiger bei stack->stackSize = parentest.length; dereferenzieren, werden Sie einen Segmentierungsfehler haben. Diese char parentest[paren.length] = paren; ist auch falsch.

Es ist fast nichts korrekt über den Code, den Sie angezeigt haben.

Dies ist ein Beispiel dafür, wie Sie Ihre Klasse müssen instanziiert und wie Member-Funktionen aufzurufen:

StringStack stack(100); 
string s("a string"); 
stack.push(s); 
+0

Vielen Dank für Ihre Hilfe. Ich verstehe, was ich jetzt falsch mache. – starlight