c++ - 我编写了一个用于检查括号的代码,但编译器向我显示了这些错误
问题描述
#include<bits/stdc++.h>
using namespace std;
class stack
{
public:
int size;
int top;
char * arr;
};
int isempty(stack*ptr)
{ if (ptr->top==-1)
{
return 1;
}
return 0;
}
int isFull(stack*ptr)
{
if (ptr->top==ptr->size-1)
{
return 1;
}
else
{
return 0;
}
}
void push(stack*ptr,char value)
{
if(isFull(ptr))
{
cout<<"stack overflow";
}
else
{
ptr->top++;
ptr->arr[ptr->top]=value;
}
}
char pop(stack*ptr)
{
if(isempty(ptr))
{
cout<<"Stack is empty";
return -1;
}
else
{
char v=ptr->arr[ptr->top];
ptr->top--;
return v;
}
}
int peek(stack*ptr,int i)
{
if(ptr->top-i+1<0)
{
cout<<"invalid input";
}
else
{
return ptr->arr[ptr->top-i+1];
}
}
int paranthesisamatch(char*exp)
{ stack*ptr;
ptr->size=100;
ptr->top=-1;
ptr->arr=(char *)malloc(ptr->size * sizeof(char));
for (int i = 0;exp[i]!="\0"; i++)
{
if(exp[i]=='(')
{
push(ptr,'(');
}
else if (exp[i]==')')
{
if (isempty(ptr))
{
return 0;
}
pop(ptr);
}
{
if (isempty(ptr))
{
return 1;
}
return 0;
}
int main()
{
char*exp= "8+(9*4)";
if (paranthesisamatch(exp)))
{
cout<<"The paranthesis is matching";
}
cout<<"The paranthesis is not matching";
return 0;
};
'错误'
C:\Users\91977\Desktop\C++>cd "c:\Users\91977\Desktop\C++\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\91977\Desktop\C++\"tempCodeRunnerFile
tempCodeRunnerFile.cpp:12:13: error: reference to 'stack' is ambiguous
int isempty(stack*ptr)
^~~~~
tempCodeRunnerFile.cpp:3:7: note: candidates are: class stack
class stack
^~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stack:61:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\mingw32\bits\stdc++.h:89,
from tempCodeRunnerFile.cpp:1:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_stack.h:99:11: note: template<class _Tp, class _Sequence> class std::stack
class stack
^~~~~
tempCodeRunnerFile.cp:12:19: error: 'ptr' was not declared in this scope
int isempty(stack*ptr)
^~~
解决方案
看来您的堆栈类与 std::stack 冲突。因为 std 已经有堆栈类,并且您的类与标准库堆栈具有相同的名称,所以在这种情况下编译器不知道使用哪个堆栈,所以我建议您不要使用:
using namespace std;
因为这是不好的做法,也请不要包含 #include<bits/stdc++.h>因为这也是不好的做法。
有关#include<bits/stdc++.h>的更多信息 在这里。
但使用 std:: 前缀与你想要的 std 类。
推荐阅读
- jenkins - 状态为 ABORTED 时,Jenkins Pipeline Post Action sshPublisher 不工作
- android - JUnit/Mockito:java.lang.IllegalStateException:“字段”不能为空
- ios - 在运行时更新 UITabBarController 项目标题
- python - Pandas 按一列和索引排序
- c# - 为什么将 VB.NET 代码迁移到 C# 时,for 循环的行为会有所不同?
- loops - SAS 迭代是如何通过变量 _N_ 工作的,如果有的话
- python - 在python中将几个变量阈值成二进制分类代码
- c++ - 为什么不对指针强制执行 constness?
- php - 在自定义 magento 2 模块中使用外部 php 库的最简单方法是什么?
- node.js - TrueVault BLOB 到节点中的文件