如何解决为什么这段代码编译失败?
// BalancedStrings.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <stack>
#include "stdafx.h"
using namespace std;
bool isBalanced(string s) {
stack<char> stack;
char l;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
stack.push(s[i]);
continue;
}
if (stack.empty())
return false;
switch (s[i]) {
case ')':
l = stack.top();
stack.pop();
if (l == '{' || l == '[')
return false;
case '}':
l = stack.top();
stack.pop();
if (l == '(' || l == '[')
return false;
break;
case ']':
l = stack.top();
stack.pop();
if (l == '{' || l == '(')
return false;
break;
}
}
return true;
}
int main()
{
string s1 = "{}";
std::cout << isBalanced(s1);
return 0;
}
然而,当我试图编译这段代码时,我遇到了很多编译错误,比如 'C2039'cout': is not a member of 'std',C2065 'string': undeclared identifier 等。我能够得到通过将标题 #include "stdafx.h" 移动到顶部来编译代码。所以我想更深入地了解,如何改变头文件的顺序才能让我的代码编译成功。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。