我不明白这是什么错误是在我的C++说代码无法将字符串转换为'char *'参数错误?
error: cannot convert 'std::string {aka std::basic_string}' to 'char' for argument '1' to 'bool areParenthesesBalanced(char*)'
if(areParenthesesBalanced(str))
我的代码:
bool areParenthesesBalanced(char expr[])
{
stack<char> s;
char a, b, c;
for (int i=0; i<strlen(expr); i++)
{
if (expr[i]=='(' || expr[i]=='[' || expr[i]=='{')
{
s.push (expr[i]);
}
else
{
switch (expr[i])
{
case ')':
a = s.top();
s.pop();
if (a=='{' || a=='[')
cout<<"Not Balanced";
break;
case '}':
b = s.top();
s.pop();
if (b=='(' || b=='[')
cout<<"Not Balanced";
break;
case ']':
c = s.top();
s.pop();
if (c=='(' || c=='{')
cout<<"Not Balanced";
break;
}
}
}
if (s.empty()) //check if stack is empty
{
return true;
}
else
{
return false;
}
}
int main()
{
std::ifstream t("file.txt");
std::string str;
t.seekg(0, std::ios::end);
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);
str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
if(areParenthesesBalanced(str))
{
cout<<"Balanced";
}
else
{
cout<<"Not Balanced";
}
return 0;
}
我不明白什么是看到的问题!有任何想法吗?
为什么expr是一个char数组而不是std :: string?你为什么用std :: string调用你的函数? –