【发布时间】:2017-12-29 07:48:42
【问题描述】:
我不明白这个错误在我的 C++ 代码中的含义
错误:无法将 'std::string {aka std::basic_string}' 转换为 'char' 用于参数 '1' 到 '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 调用你的函数?
标签: c++ compiler-errors char boolean