简体   繁体   中英

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

I declare a string parameter inside of a bool function. And when I run the code build messages show me "string was not declare in the scope".

I tried it on code block ..

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;
}

The error message is "String was not declare in the scope"

You didn't add using namespace std; in your program. I compiled it successfully in codeblocks after adding using namespace std; .

Here is the full code:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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