代码不工作.使用栈

Code is not working. Using Stacks

本文关键字:工作 代码      更新时间:2023-10-16

这是我检查平衡括号的代码,它接受一个表达式并检查用户是否正确输入了表达式,但它不工作。它给出了一个错误。我不认为在公共事务上有什么错误。请帮助!

class dynamicStack {
    struct node{
        char num;
        node *next;
    };
    public:
    node *top;
    dynamicStack(){
        top=NULL;
    }
    void push(char);
    void pop();
    void check(string);
};
void check(string exp) {
    \some code
}
void dynamicStack::pop(){
    node *temp;
    temp=top;
    if(top == NULL)  {
        cout<<"Stack is empty"<<endl;
    }
    else
        cout<<"Deleting number: "<<temp->num<<endl;
    top=top->next;
    delete temp;
}
void dynamicStack::push(char c) {
    node *newNode;
    newNode = new node;
    newNode->num=c;
    newNode->next=top;
    top=newNode;
}
int _tmain(int argc, _TCHAR* argv[])  {
    dynamicStack dS;
    string exp;
    cout<<"Enter an expression:  ";
    cin>>exp;
    dS.check(exp);
    system("pause");
    return 0;
}

给出如下错误:

     1>ds-2.obj : error LNK2019: unresolved external symbol "public: void                    _thiscall dynamicStack::check(class std::basic_string<char,struct                      std::char_traits<char>,class std::allocator<char> >)" (?    check@dynamicStack@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _wmain

你的实现

void check(string exp)

没有提到它的类作用域。它必须是:

void dynamicStack::check(string exp) {
...
}

顺便说一句,这正是链接器消息试图告诉你的。当你得到这类错误时,你通常只是把上面的东西搞错了。

Member functions(程序中的check函数)可以在类定义中定义,也可以使用scope resolution operator::在类外单独定义。如果想在类之外定义某个函数,可以使用作用域解析操作符:::来实现,如下所示:

void dynamicStack :: check(string exp)
{
    //Do something
}

在你的程序中,你忘记了函数check()的范围解析运算符。您提供的错误(unresolved external symbol)是因为您一直在使用ds.check()调用dS对象的成员函数,但编译器没有找到成员函数check()的实现。没有作用域解析操作符的函数定义将被视为单独的函数。

在类定义中定义成员函数声明该函数为内联函数,即使不使用内联说明符也是如此。