Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用堆栈匹配程序的括号(非STL模板堆栈)_C++_Reference_Static_Stack_Member - Fatal编程技术网

C++ 使用堆栈匹配程序的括号(非STL模板堆栈)

C++ 使用堆栈匹配程序的括号(非STL模板堆栈),c++,reference,static,stack,member,C++,Reference,Static,Stack,Member,我正在为我的家庭作业做一个括号匹配的程序,我不太清楚该怎么做。调用堆栈函数时出现错误,表示必须使用is_balanced函数中特定对象的相对值进行非静态成员引用。为什么会发生此错误?我如何修复它?谢谢你的帮助 #include <iostream> #include <string> #include "StringStack.h" using namespace std; char parentest[]; int main() { //v

我正在为我的家庭作业做一个括号匹配的程序,我不太清楚该怎么做。调用堆栈函数时出现错误,表示必须使用is_balanced函数中特定对象的相对值进行非静态成员引用。为什么会发生此错误?我如何修复它?谢谢你的帮助

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

}
#包括
#包括
#包括“StringStack.h”
使用名称空间std;
char parentest[];
int main(){
//变数
字符串paren;
StringStack*stack;
//用户输入
cout>paren;
//将字符串转换为字符数组
char parentest[paren.length]=paren;
//将堆栈的大小设置为字符数组的长度。
堆栈->堆栈大小=parentest.length;
//显示论文的字符串

cout因为您调用的非静态成员函数就像它们是静态的一样。尽管您有一个StringStack指针,但您也从不分配StringStack的实例。当您在
stack->stackSize=parentest.length;
处取消引用该指针时,您将出现分段错误。 这
char parentest[paren.length]=paren;
也是错误的

您显示的代码几乎没有任何正确之处

这是您需要如何实例化类以及如何调用成员函数的示例:

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

谢谢你的帮助。我明白我现在做错了什么。