繁体   English   中英

我收到“错误:字符串未在此范围内声明”

[英]I'm getting an "Error: string was not declared in this scope"

我在 bool 函数中声明了一个字符串参数。 当我运行代码构建消息时,显示“字符串未在范围内声明”。

我在代码块上试过..

bool isOkay(char open,char close);
bool isCheck(string exp);

bool isCheck(string exp){
    stack<char>s;
    int i;

    for(i=0;i<exp.length();i++){
        if(exp[i] == '(' || exp[i] == '{' || exp[i] == '['){
            s.push(exp[i]);
        }
        else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']'){
            if(s.empty() || !isOkay(s.top(),exp[i])){
                return false;
            }
            else{
                s.pop();
            }
        }
    }
    return s.empty() ? true : false;
}

bool isOkay(char open , char close){
    if(open == '(' && close== ')') return true;
     else if(open == '{' && close== '}') return true;
      else if(open == '[' && close== ']') return true;

    return false;
}

错误消息是“字符串未在范围内声明”

你没有添加using namespace std; 在你的程序中。 添加使用命名空间 std后,我在代码块中成功编译了它 .

这是完整的代码:

#include <iostream>
#include <string>
#include <stack>

using namespace std; // May be you didn't write this line.

bool isOkay(char open,char close);
bool isCheck(string exp);

int main() {
    // main func code goes here

    return 0;
}

bool isCheck(string exp){
    stack<char>s;
    int i;

    for(i=0;i<exp.length();i++){
        if(exp[i] == '(' || exp[i] == '{' || exp[i] == '['){
            s.push(exp[i]);
        }
        else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']'){
            if(s.empty() || !isOkay(s.top(),exp[i])){
                return false;
            }
            else{
                s.pop();
            }
        }
    }
    return s.empty() ? true : false;
}

bool isOkay(char open , char close){
    if(open == '(' && close == ')') return true;
     else if(open == '{' && close == '}') return true;
      else if(open == '[' && close == ']') return true;

    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM