为什么这段代码编译失败?

时间:2021-07-03 13:04:08

标签: c++ visual-studio visual-c++

我正在尝试使用 C++ 并在下面的代码段中编写了此代码-

 // 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" 移动到顶部来编译代码。所以我想更深入地了解,如何改变头文件的顺序才能让我的代码编译成功。

0 个答案:

没有答案
相关问题