代码不工作.使用栈
Code is not working. Using Stacks
这是我检查平衡括号的代码,它接受一个表达式并检查用户是否正确输入了表达式,但它不工作。它给出了一个错误。我不认为在公共事务上有什么错误。请帮助!
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()
的实现。没有作用域解析操作符的函数定义将被视为单独的函数。
在类定义中定义成员函数声明该函数为内联函数,即使不使用内联说明符也是如此。
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- C++代码在台式机上工作正常,但在笔记本电脑上则不行
- 我想使用此代码单击某个特定窗口,但它无法正常工作
- C++代码停止工作错误使用cout内部函数
- 我的代码无法正常工作。谁能指出错误?
- 有人可以详细解释这个回文代码是如何工作的吗?
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 代码在Visual Studio 2017中不起作用,但在VS代码中工作
- 以下代码如何工作以每次为唯一调用堆栈唯一实例化模板函数?
- 添加新行时工作代码引发异常.调试技巧?
- 一个非常简单的win32套接字代码,但工作错误
- 需要WXLISTCTRL的代码更改为虚拟样式WXListCtrl的工作代码
- C++ / CannyEdgeDetection.exe 已停止工作 代码块 /OpenCV 错误:断言失败
- C //尝试重写一个工作代码以包括我的功能
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 如何在不破坏当前工作代码的情况下添加小数
- 无法理解工作代码和损坏代码之间的区别
- .exe已停止工作代码块 CPP
- rapidjson:用于从文件中读取文档的工作代码