2016-04-06 5 views
0

私は宿題のためのカッコのマッチングプログラムを作成しています。 is_balanced関数内の特定の相対オブジェクトを使用して非静的メンバ参照を行う必要があるというスタック関数を呼び出すときにエラーが発生します。このエラーはなぜ発生し、どのように修正できますか?助けてくれてありがとう。スタック(STLテンプレートスタックではない)を使用してプログラムにマッチする括弧

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

} 

答えて

0

静的でないメンバー関数を静的であるかのように呼び出すためです。また、StringStackポインタを持っていても、StringStackのインスタンスは割り当てられません。そのポインタをstack->stackSize = parentest.length;に参照解除すると、セグメント化エラーが発生します。 このchar parentest[paren.length] = paren;も間違っています。

あなたが示したコードについては、ほとんど何も正しくありません。

これはあなたのクラスをインスタンス化すると、メンバ関数を呼び出す方法を必要とする方法の例です:

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

はあなたの助けをいただき、ありがとうございます。私は今何が間違っているのか理解しています。 – starlight

関連する問題