我收到 C++ 禁止比较指针与字符的错误
无论我将 s.at(i) 与任何东西进行比较,都会出现某种 -fpermissive 错误。
string s;
cout<<"Enter the expression";
getline(cin,s);
int i;
for(i=0;i<s.length();i++)
{
if(s.at(i)=="("||s.at(i)=="["||s.at(i)=="{")
push(s.at(i));
}
for(i=0;i<s.length();i++)
{
if(s.at(i)=="]"||s.at(i)==")"||s.at(i)=="}")
{
x=pop();
if (s.at(i)==x)
continue;
else
{enter code here
cout<<"\nInvalid expression";
return 0;
}
}
}
最佳答案
考虑 s.at(i)=="("
.
文字 "("
实际上是一个 const char[2]
类型(一个元素用于 (
,另一个用于 NUL 终止符),衰减 为 const char*
某些情况下的指针,例如使用 ==
时的这种情况.
s.at(i)
返回单个 char
类型,正在与 const char*
进行比较指针。您有用的编译器警告您此错误。
解决方案很简单:使用'('
相反,它是一个 char
文字。你可以比较一个char
到 char
.
关于c++ - 将 s.at(i) 与一个字符进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246055/